Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ignore a UIGestureRecognizer touch? [closed]

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

like image 403
SpokaneDude Avatar asked Apr 19 '13 18:04

SpokaneDude


2 Answers

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.

like image 143
jszumski Avatar answered Sep 27 '22 18:09

jszumski


What do you mean process? The touch is there, what you do with it, is up to you. If you don't want to do anything with it, just don't, otherwise do something. The idea of wanting to ignore is up to your logic...

I stumble with this question about this, but either you putted the question in the wrong terms, or you want something else: UIGestureRecognizer blocks subview for handling touch events

Edit1:

I mean I want to ignore the tap because it's for another UIView

Then you are adding the Tap Gesture in the wrong UIView.

like image 40
Rui Peres Avatar answered Sep 27 '22 18:09

Rui Peres