I have created a small UIView
which contains two UIButton
s. The view responds to UITapGesture
events. The Buttons are supposed to respond to TouchUpInside
, however when I tap the buttons the responder is the underlying view and the tap gesture selector is triggered. Looking for advice or suggestions.
-
What I've ended up doing is trading the UIButtons for UIViews that also have an attached tap gesture recognizer. This seems to work the way I expected the buttons to behave. – Kyle Feb 07 '11 at 16:14
6 Answers
The right answer (which prevents the tabrecognizer from highjacking any taps and doesn't need you to implement a delegate etc) is found here. I got a lead to this answer via this post.
In short use:
tapRecognizer.cancelsTouchesInView = NO;
"Which prevents the tap recognizer to be the only one to catch all the taps"
You can modify the method that responds to the tap gesture in the orange view:
-(void) handleTapFrom:(UITapGestureRecognizer*) recognizer {
CGPoint location = [recognizer locationInView:orangeView];
UIView *hitView = [orangeView hitTest:location withEvent:nil];
if ([hitView isKindOfClass:[UIButton class]]) {
return;
}
//code that handle orange view tap
...
}
This way if you touch a UIButton, the tap will be ignored by the underlying view.

- 20,218
- 7
- 70
- 53
How are the views ordered in the view hierarchy? Also, are you creating the interface in IB? If you hook up the connections properly, this shouldn't be an issue at all...

- 16,250
- 7
- 45
- 84
The problem with this design is that it's similar to embedding one button into another. While you may have a valid reason to do it, you should be more careful with the event flow.
What happens is the gesture recognizer intercepting touches before they reach subviews (the two buttons in your case). You should implement UIGestureRecognizerDelegate protocol, namely, gestureRecognizer:shouldReceiveTouch:
method, and return NO if the touch is inside any of the two buttons. That will prevent the orange view from usurping touches intended for the buttons and the buttons will start to work as expected.

- 23,712
- 4
- 76
- 79
-
I thought of this however the view that is responding to the touch is the underlying view even though the animation (blue flash on the button) shows the button responding. – Kyle Feb 05 '11 at 19:30