如何在iOS中处理1到3个手指轻扫手势

atb*_*btg 8 iphone objective-c ios swipe-gesture

我使用以下代码处理我的代码中的1个手指滑动:

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];
    [swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
    [swipe setDelaysTouchesBegan:YES];
    [[self view] addGestureRecognizer:swipe];
Run Code Online (Sandbox Code Playgroud)

我知道我可以添加以下行以使其处理2个手指滑动:

 [swipe setNumberOfTouchesRequired:2];
Run Code Online (Sandbox Code Playgroud)

但是,当我添加上面的代码时,由于现在所需的触摸次数为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];
Run Code Online (Sandbox Code Playgroud)

谢谢.

Mob*_*ord 12

在handleViewsSwipe中,您可以从手势识别器获取numberOfTouches属性.

- (void)handleViewsSwipe:(UISwipeGestureRecognizer *)recognizer {
    NSUInteger touches = recognizer.numberOfTouches;
    switch (touches) {
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        default:
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)

根据您获得的触摸次数,只需切换相同的方法即可.


rob*_*off 10

在视图中添加三个滑动手势识别器:

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];
}
Run Code Online (Sandbox Code Playgroud)

为我工作.