1
votes

I have a cutsom UITableViewCell implemention. I have registered this subclass of UITableViewCell for a UIPanGestureRecognizer which i use to swiping the cells to the right or left.

// in the UITableViewCell subclass :

 UIGestureRecognizer* recognizer = 
 [[UIPanGestureRecognizer alloc] initWithTarget:
 self   
 action:@selector(handlePan:)];

    recognizer.delegate = self;
    [self addGestureRecognizer:recognizer];
       recognizer.cancelsTouchesInView = NO;

Now I want to to present a view controller when the user does a two finger swipe "up" on the screen. So, I added a UISwipeGestureRecognizer to the tableview.

// code in the view controller containing the tableview reference.

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleViewsSwipe:)];

            [swipe setDirection:UISwipeGestureRecognizerDirectionUp];
            [swipe setDelaysTouchesBegan:NO];
            [[self tableView ]addGestureRecognizer:swipe];
            swipe.cancelsTouchesInView= YES;
            [swipe setNumberOfTouchesRequired:2];
            swipe.delegate = self;
            self.tableView.multipleTouchEnabled = YES;

But when I do a two finger swipe on the screen, the pan gesture gets triggered . How can I solve this ?

1
That may be because you haven't set the maximumNumberOfTouches property for the UIPanGestureRecognizer. Set recognizer.maximumNumberOfTouches = 1; - sooper

1 Answers

0
votes

As sooper's says, setting the maximumNumberOfTouches = 1 will probably work.

For others trying to deal with 2 gestureRecognizers at the same time that are both 1 touch gestures I found that making sure to set this delegate to yes

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

and then in the gesture recognizer action you can check for a certain translation or whatever you need and cancel one of the gesture recognizers.

Such as:

- (void)panSwipeRecognizer:(UIPanGestureRecognizer*)panRecognizer
{
    CGPoint translation = [panRecognizer translationInView:self.superview];

    if(panRecognizer.state == UIGestureRecognizerStateBegan)
    {
        if(fabsf(translation.x) < fabsf(translation.y))
        {
            //deactivate horizontal gesture recognizer
            panRecognizer.enabled = NO;
            panRecognizer.enabled = YES;
        }
        else //if(fabsf(translation.x) > fabsf(translation.y))
        {
            //deactivate vertical gesture recognizer
            otherGestureRecognizer.enabled = NO;
            otherGestureRecognizer.enabled = YES;
        }
    }
    //other statements like stateChanged and stateBegan
}