【发布时间】:2017-08-19 18:41:47
【问题描述】:
如何通过 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");
}
}
【问题讨论】:
-
您应该展示一些您尝试过但没有工作的代码,如果您没有尝试任何东西,我建议您在询问之前先做:)
-
请在下面查看我的答案。
-
@TallentSiu 我为你添加了一个工作代码。请在下面查看。如果您发现它有效,我很感激投票和/或接受答案。 :) 乐于助人。
标签: ios objective-c xcode uiview uibackgroundcolor