How do I ignore a UIGestureRecognizer touch?

ios, objective-c, uigesturerecognizer

Solution

If you want to prevent the recognizer from receiving the touch at all, `UIGestureRecognizerDelegate` has a method `gestureRecognizer:shouldReceiveTouch:` you can use:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (/* some logic here*/) {
        return NO;
    }

    return YES;
}

If you have multiple recognizers in play, you should check to see if the `gestureRecognizer` is your tap recognizer before doing your test logic.

If you must process all touches, it looks like you need to call `ignoreTouch:forEvent:` with both arguments:

    [recognizer ignoreTouch:someTouch withEvent:someEvent];

This method seems to be intended only for `UIGestureRecognizer` subclasses to use, so I would suggest making a subclass of `UITapGestureRecognizer` that implements your own custom logic for determining the validity of taps.

Problem

I'm using `UIGestureRecognizer` to capture taps... if I don't want to process a particular tap, the manuals says: "If a gesture recognizer detects a touch that it determines is not part of its gesture, it can pass the touch directly to its view. To do this, the gesture recognizer calls ignoreTouch:forEvent: on itself, passing in the touch object." Unfortunately, I can't find any example of using this. This is my code in the `UIGestureRecognizer` handler: ``` - (void)singleFingerTap:(UITapGestureRecognizer*)gesture { CGPoint pt = [gesture locationInView:self.view]; CGRect dataRect = CGRectMake(117.0,416.0,670.0,1450); CGPoint dataPoint = CGPointMake(pt.x, pt.y); // check to see if point is within the rectangle if(!CGRectContainsPoint(dataRect, dataPoint)) { NSLog(@"\n\nNOT within subViewData (x: %f y: %f",dataPoint.x, dataPoint.y); [self.view ignoreTouch:gesture]; } else { NSLog(@"\n\nIS within subViewData(x: %f y: %f",dataPoint.x, dataPoint.y); } } ``` I keep getting an error: No visible @interface for 'UIView' declares the selector 'ignoreTouch:' I have read the App docs and they have what I quoted; nothing on SO or Google that answers this question. Help is very much appreciated (as usual). :D

Original source

Related problems