how to handle 1 to 3 fingers swipe gesture in iOS

12,507

Solution 1

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.

Solution 2

Add three swipe gesture recognizers to your view:

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

Worked for me.

Share:
12,507
atbebtg
Author by

atbebtg

Updated on June 05, 2022

Comments

  • atbebtg
    atbebtg almost 2 years

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

    I know i can add the following line to make it handle 2 fingers swipe:

     [swipe setNumberOfTouchesRequired:2];
    

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

    Thank you.

  • atbebtg
    atbebtg over 12 years
    I added the above code but the handleViewsSwipe only fires when touches is 1. If I try to use 2 fingers or 3 fingers to swipe it never get called. Any idea?
  • MobileOverlord
    MobileOverlord over 12 years
    In addition to this code you will need to do what rob said and add 3 gesture recognizers to the view. one for each number of fingers. all of which can refer to the same method.
  • Ranjit
    Ranjit over 10 years
    Hello @rob mayoff please look at my question stackoverflow.com/questions/20761686/… I am struggling to solve this. Please help me out. Whether this can be solved using UIPangesture Recogniser