How do you stop UITapGestureRecognizer from catching EVERY tap?
Asked Answered
M

2

31

Hello I have an opengl view and on that I have a tab bar. I'm using a tap recognizer to tap different 3d objects on screen. In the tab bar I have a button but it doesn't work because the tap recognizer catches these taps too. How do I stop this? I've already tried this:


- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
  if ([touch.view isKindOfClass:[UIBarButtonItem class]]) return FALSE;
  return TRUE;
}

I think I am somehow comparing wrong classess because when I debug it returns TRUE always.

Mort answered 3/2, 2011 at 11:43 Comment(2)
your if-statement can never return false - a button is not a view... But you could check via a breakpoint in the debugger which type the view is you get on different tap positions.Your
Oh..I just noticed that UIBarButtonItem is not an UIView :) Thanks.Mort
C
33

Or you can just do [singleTap setCancelsTouchesInView:NO]. Example:

UITapGestureRecognizer *singleTap = [
    [UITapGestureRecognizer alloc]
    initWithTarget: self
    action: @selector(yourSelector:)
];
[singleTap setCancelsTouchesInView:NO];
[[self view] addGestureRecognizer: singleTap];
Cappello answered 10/7, 2012 at 18:39 Comment(2)
Also available in the Interface Builder as Canceled in View. Great!Laurice
Only thing that worked. And like a charm, might I add! no messing with the rest of the code. thks!Overthrust
M
28
  if ([touch.view.superview isKindOfClass:[UIToolbar class]]) return FALSE;

This is how I got it to work. The superview is a UIToolbar, probably UIBarButtonIttem is a view after all.

Mort answered 3/2, 2011 at 12:29 Comment(1)
This worked where hit testing on the gesture recognizer's view failed. To be clear, I was working within a UITableView and the gesture's location was reported as {0, contentOffset - someConstant}. Checking the touch's view is a good solution. Thanks.Midwinter

© 2022 - 2024 — McMap. All rights reserved.