6

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

Rob
  • 415,655
  • 72
  • 787
  • 1,044
SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • You quoted docs that say "calls `ignoreTouch:****forEvent:*****`" on a ***gesture recognizer***, and you're wondering calling `ignoreTouch:` on a _view_ doesn't work?! What did you expect to happen? You have [so many questions](http://stackoverflow.com/search?q=user%3A1231786+selector) that you could answer just by reading the error message and looking at your code. – jscs Apr 19 '13 at 20:40

2 Answers2

9

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.

jszumski
  • 7,430
  • 11
  • 40
  • 53
  • No, I have to be able to receive all of the touches, then sort out what I don't want. Supposedly -ignoreTouch:forEvent will do what I want it to do, but I'm not sure I have it coded correctly. – SpokaneDude Apr 19 '13 at 18:28
  • @jszumski `ignoreTouch:forEvent:` is a `UIGestureRecognizer` method and not a `UIView` one. – Rui Peres Apr 19 '13 at 18:35
  • Jacky Boy: isn't that where I have it in my code above? – SpokaneDude Apr 19 '13 at 18:37
  • 1
    He is sending `ignoreTouch:forEvent:`to a `UIView` and with the wrong parameters. – Rui Peres Apr 19 '13 at 18:39
  • @spokane-dude I think your best bet is to subclass in this case. – jszumski Apr 19 '13 at 18:41
  • I am keeping my -1, because you are leading spokane badly... Careful... The first parameter is a `UITouch` and not a "some gesture"... – Rui Peres Apr 19 '13 at 18:46
  • @JackyBoy: according to the docs: ...the gesture recognizer calls ignoreTouch:forEvent: on itself, passing in the touch object.". So given my code above (which is the GestureRecognizer), what is the "ignoreTouch" supposed to be? – SpokaneDude Apr 19 '13 at 18:49
  • Actually it passes a `UITouch`, that has the `UIView` touched as property... So it does a bit more than just passing the `UIView` touched – Rui Peres Apr 19 '13 at 18:58
  • @JackyBoy For a tap recognizer the touch is the gesture, but its semantic at this point without a class attached to it, so we can leave it at that. – jszumski Apr 19 '13 at 18:59
  • So, what am I supposed to do? I'm in my gesture recognizer and I want to correctly ignore a touch... according to the docs... so how am I supposed to do it? – SpokaneDude Apr 19 '13 at 19:01
  • As jszumski said, sub-class the `UIGestureRecognizer`. – Rui Peres Apr 19 '13 at 19:02
1

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.

Community
  • 1
  • 1
Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • By "process", I mean I want to ignore the tap because it's for another UIView. However, what's happening is once I handle any tap, the other methods in the normal flow don't get executed. I tried just a simple "return" statement, but still, nothing else gets executed. – SpokaneDude Apr 19 '13 at 18:43