1

如何通过 SwipeGesture 更改 UIView 背景颜色?例如,绿色用于向左滑动,红色用于向右滑动。非常感谢。

代码不起作用:

- (void)viewDidLoad {

    self.view.backgroundColor=[[UIColor alloc]initWithRed:24.0/255 green:96.0/255 blue:120.0/255 alpha:0.5];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)];
    [imageView setUserInteractionEnabled:YES];

    [super viewDidLoad];

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

    // Setting the swipe direction.
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

    // Adding the swipe gesture on image view
    [self.view addGestureRecognizer:swipeLeft];
    [self.view addGestureRecognizer:swipeRight];
}

    - (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {

        if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {

            self.view.backgroundColor=[[UIColor alloc]initWithRed:138.0/255 green:24.0/255 blue:47.0/255 alpha:1.0];
            NSLog(@"Left Swipe");
        }

        if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {

            self.view.backgroundColor=[[UIColor alloc]initWithRed:24.0/255 green:96.0/255 blue:120.0/255 alpha:1.0];
            NSLog(@"Right Swipe");
        }

    }
4

2 回答 2

1

You need to have separate swipe handler to make it work. This works great.

Your codes in setting up your UISwipeGestureRecognizer should look like this.

 UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];

    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    imageView.userInteractionEnabled = YES;
    // Adding the swipe gesture on image view
    [imageView addGestureRecognizer:swipeRight];
    [imageView addGestureRecognizer:swipeLeft];

And your swipe handle should be like this.

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)swipe {
    self.view.backgroundColor=[[UIColor alloc]initWithRed:24.0/255 green:96.0/255 blue:120.0/255 alpha:1.0];
}

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)swipe {
    self.view.backgroundColor=[[UIColor alloc]initWithRed:138.0/255 green:24.0/255 blue:47.0/255 alpha:1.0];
}
于 2017-03-28T05:15:29.427 回答
0

interface在 ViewController 文件中声明一个 imageView 属性。@property (weak, nonatomic) UIImageView *imageView;

- (void)viewDidLoad
    {
    [super viewDidLoad];
  self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)];
    [imageView setUserInteractionEnabled:YES];

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

    // Setting the swipe direction.
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];

    // Adding the swipe gesture on image view
    [self.imageView addGestureRecognizer:swipeLeft];
    [self.imageView addGestureRecognizer:swipeRight];

    }

处理滑动手势事件 //newImage 和 newImage1 是您projectImageAssets - (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        NSLog(@"Left Swipe");
self.view.backgroundColor = [UIColor greenColor];
self.imageView.image = [UIImage imageNamed: @"myNewImage.png"];
    }

    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"Right Swipe");   
    self.view.backgroundColor = [UIColor redColor];
    self.imageView.image = [UIImage imageNamed: @"myNewImage1.png"];
    } 

}

希望这可以帮助。快乐编码。请参阅此 SO 帖子

于 2017-03-27T07:15:24.190 回答