Search code examples
objective-cipaduibuttonuigesturerecognizeribooks

Using UITapGestureRecognizer to show/hide menus


I'm trying to find a good way to make a tap show/hide my menus, in the same way as iBooks. I set up UITapGestureRecognizer to do this, but unfortunately it means then that none of the buttons on the screen work. I know I can use

tapRecognizer.cancelsTouchesInView = NO;

in order to allow the touches to go through, but then of course, both things happen and I can't cancel the menu show/hide.

Is there a way for me to stop the UITapGestureRecognizer from happening on certain buttons or actions? Or is there a better way of doing this in general?

Thanks very much in advance for any help you can give me!

:-Joe


Solution

  • In case someone needs a little code to understand Joe's solution, you should first implement the UIGestureRecognizerDelegate protocol (i.e @interface YourViewController : UIViewController <UIGestureRecognizerDelegate>).

    In the class that implements the UIGestureRecognizerDelegate protocol, you have to implement - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch. There, you can check if touch.view is a button. In my implementation, I only used a return !([touch.view isKindOfClass:[UIButton class]]); but you may need to extend this if you have other touch-enabled components in which you don't want to trigger your tap recognizer.