[英]how to handle 1 to 3 fingers swipe gesture in iOS
我使用以下代码处理我的代码中的1个手指滑动:
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipe setDelaysTouchesBegan:YES];
[[self view] addGestureRecognizer:swipe];
我知道我可以添加以下行以使其处理2个手指滑动:
[swipe setNumberOfTouchesRequired:2];
但是,当我添加上面的代码时,由于现在所需的触摸次数为2,因此不再检测到手指滑动。我可以做些什么来使我的代码适用于1,2或3个手指滑动?
我尝试使用以下代码,但这不符合我的要求。
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:3];
[panRecognizer setDelaysTouchesBegan:YES];
[[self view] addGestureRecognizer:panRecognizer];
[panRecognizer release];
谢谢。
在handleViewsSwipe中,您可以从手势识别器获取numberOfTouches属性。
- (void)handleViewsSwipe:(UISwipeGestureRecognizer *)recognizer {
NSUInteger touches = recognizer.numberOfTouches;
switch (touches) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
}
根据您获得的触摸次数,只需切换相同的方法即可。
在视图中添加三个滑动手势识别器:
for (int i = 1; i <= 3; ++i) {
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];
swipe.numberOfTouchesRequired = i;
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
swipe.delaysTouchesBegan = YES;
[self.view addGestureRecognizer:swipe];
}
为我工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.