본문 바로가기

iPhone dev./UIView, UITableView

UITapGestureRecognizer를 view에 적용했을 때 view의 button들이 먹통되는 현상

Stackoverflow.com 참조


I have view with a UITapGestureRecognizer. So when I tap on the view an other view appears above this view. This new view has three buttons. When I now press on one of these buttons I don't get the buttons action, I only get the tap gesture action. So I'm not able to use these buttons anymore. What can I do to get the events through to these buttons? The weird thing is that the buttons still get highlighted.

I can't just remove the UITapGestureRecognizer after I received it's tap. Because with it the new view can also be removed. Means I want a behavior like the fullscreen vide controls.

link|edit

feedback

You can set your controller or view (whichever creates the gesture recognizer) as the delegate of theUITapGestureRecognizer. Then in the delegate you can implement -gestureRecognizer:shouldReceiveTouch:. In your implementation you can test if the touch belongs to your new subview, and if it does, instruct the gesture recognizer to ignore it. Something like the following:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
   
// test if our control subview is on-screen
   
if (self.controlSubview.superview != nil) {
       
if ([touch.view isDescendantOfView:self.controlSubview]) {
           
// we touched our control surface
           
return NO; // ignore the touch
       
}
   
}
   
return YES; // handle the touch
}
link|edit
In my header file, i had my view implement UIGestoreRegognizerDelegate, and in my .m i added your code above. The tap never enters this method, it goes straight to my handler. Any ideas? – kmehta Apr 29 '11 at 21:50
@kmehta you most likely forgot to set the UIGestureRecognizer delegate property. – Till May 10 '11 at 1:38
Can this answer be generalized to handle all cases where there is an IBAction involved? – Martin Wickman Aug 3 '11 at 8:54
@Martin IBAction compiles down to void so it doesn't leave any information behind at runtime to use for detection. But what you can do is walk up the view hierarchy, testing for UIControl, and returning NO if you find any controls. – Kevin Ballard Aug 3 '11 at 17:38