如何在iOS中处理1到3个手指轻扫手势 [英] how to handle 1 to 3 fingers swipe gesture in iOS
问题描述
我使用以下代码处理我的代码中的1个手指滑动:
I use the following code to handle 1 finger swipe in my code:
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipe setDelaysTouchesBegan:YES];
[[self view] addGestureRecognizer:swipe];
我知道我可以添加以下行以使其处理2个手指滑动:
I know i can add the following line to make it handle 2 fingers swipe:
[swipe setNumberOfTouchesRequired:2];
然而,当我添加上面的代码时,由于所需的触摸次数,因此不再检测到手指滑动现在2.我可以做些什么来使我的代码适用于1,2或3个手指滑动?
However when I add the above code 1 finger swipe is no longer detected since the number of touches required is now 2. What can I do to make my code work for 1, 2 or 3 fingers swipe?
我尝试使用以下代码,但这不能做到我的意思我想这样做。
I tried using the following code but this doesn't do what I want to do.
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属性。
In your handleViewsSwipe you can get the numberOfTouches property from the gesture recognizer.
- (void)handleViewsSwipe:(UISwipeGestureRecognizer *)recognizer {
NSUInteger touches = recognizer.numberOfTouches;
switch (touches) {
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
}
根据具体情况,只需切换相同的方法你得了多少次接触。
Just switch the same method for what to do depending on how many touches you get.
这篇关于如何在iOS中处理1到3个手指轻扫手势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!