gpt4 book ai didi

ios - 如何在滑动时更改 UIView 背景颜色?

转载 作者:行者123 更新时间:2023-11-29 11:47:14 25 4
gpt4 key购买 nike

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

}

最佳答案

您需要有单独的滑动处理程序才能使其正常工作。这很好用。

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];
}

关于ios - 如何在滑动时更改 UIView 背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43039941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com