thomax
thomax

Reputation: 9659

UITapGestureRecognizer swallows all gestures

I'm trying to add a UITapGestureRecognizer to my app so I can detect and act upon all two finger taps, but pass any other user input through. Sounds trivial, but I just can't get it to work. Here's my code so far, inside my main view controller:

- (void) viewDidLoad {
  // other init stuff
  UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
  [tapRecognizer setNumberOfTouchesRequired:2];
  [tapRecognizer setNumberOfTapsRequired:1];
  [tapRecognizer setCancelsTouchesInView:NO];

  UIView *tapHolderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
  [tapHolderView setMultipleTouchEnabled:YES];
  [tapHolderView addGestureRecognizer:tapRecognizer];

  [self.view addSubview:tapHolderView];

  [tapRecognizer release];
}

- (void) handleTap:(UITapGestureRecognizer *) sender {
  if (sender.state == UIGestureRecognizerStateEnded) {
    // do something
  }
}

What happens is this: A two finger tap will trigger the handleTap method. Any other user input, be it swipe, pinch, single-finger-tap and so on, will not be picked up by any other view (a UIToolbar, a UIWebView amongst others).

I'd very much appreciate any insight at all on this, and please let me know if I haven't been specific enough about the problem.

Upvotes: 2

Views: 3717

Answers (3)

Christian
Christian

Reputation: 799

Try setting the view controller to be a delegate of UIGestureRecognizerDelegate, and implement gestureRecognizer:shouldRecognizeSimulaneouslyWithGestureRecgonizer to always return 'YES'. Example:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

Upvotes: 6

Warif Akhand Rishi
Warif Akhand Rishi

Reputation: 24248

We have implemented single tap and double tap gesture in same view. Hope this helps

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] 
                                     initWithTarget:self 
                                     action:@selector(handleSingleTapOnMainImageView:)];

[self addGestureRecognizer:singleTap];
[singleTap release];

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] 
                                     initWithTarget:self 
                                     action:@selector(handleDoubleTapOnMainImageView:)];
[doubleTap setNumberOfTapsRequired:2];
[singleTap requireGestureRecognizerToFail:doubleTap];
[self addGestureRecognizer:doubleTap];
[doubleTap release];

Upvotes: 0

sergio
sergio

Reputation: 69027

The only thing I can think of is the use of:

 [tapRecognizer setDelegate:self];

have you defined any delegate methods for your gesture recognizer in your view controller? would you try commenting that line out for a quick test?

Upvotes: 1

Related Questions