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.

gyozo kudor
  • 6,284
  • 10
  • 53
  • 80
  • 3
    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. – Axel Feb 03 '11 at 12:01
  • Oh..I just noticed that `UIBarButtonItem` is not an `UIView` :) Thanks. – gyozo kudor Feb 03 '11 at 12:14

2 Answers2

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];
i--
  • 4,349
  • 2
  • 27
  • 35
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.

Community
  • 1
  • 1
gyozo kudor
  • 6,284
  • 10
  • 53
  • 80
  • 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. – Justin May 04 '11 at 17:28