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.
Try setting the view controller to be a delegate of UIGestureRecognizerDelegate, and implement gestureRecognizer:shouldRecognizeSimulaneouslyWithGestureRecgonizer to always return ‘YES’. Example: