0
votes

I have created two swipe recognizers: swipeRightRecognizer & swipeLeftRecognizer and one pinch recognizer: pinchRecognizer.

When I pinch in/out, both the pinch event and the swipe left recognizer event are caught.

I have tried

[swipeLeftRecognizer requireGestureRecognizerToFail:pinchRecognizer];
[swipeRightRecognizer requireGestureRecognizerToFail:pinchRecognizer];

but it does not work.

If there is no better way, I am planning to catch the number of touches. I need to distinguish single finger swipe from pinch.

Is there a simple way that I can distinguish pinch gesture from swipe?

1
a bit old fashion: how about counting touches in touchBegan ? - Raptor
I am not too familiar with gesture recognisers. Just wondering whether there is a simple way of distinguishing pinch from swipe. If not, this is what I am planning to do, counting touches, if there is no simpler way. - aobs
I've posted a better way below. - Lyndsey Scott

1 Answers

1
votes

According to the docs requireGestureRecognizerToFail: means that first gesture only proceeds as normal if the second gesture fails; so in this case, since you haven't specified anywhere that the second gesture will fail, the first gesture will fail and the second gesture will proceed as normal. This is not what you want.

Instead you could use the shouldBeRequiredToFailByGestureRecognizer: UIGestureRecognizerDelegate method to tell the swipe gesture to fail in the case of a pinch, for example:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && 
        [otherGestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
        return YES;
    }

    return NO;
}