如何通过 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");
}
}