iosobjective-ciphonecocoa-touchuitapgesturerecognizer

UIButton inside a view that has a UITapGestureRecognizer


I have view with a UITapGestureRecognizer. So when I tap on the view another 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.


Solution

  • You can set your controller or view (whichever creates the gesture recognizer) as the delegate of the UITapGestureRecognizer. 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
    }