Gesture recognition issue in navigation bar

In my iPad app, I have several screen views.

What I want to do is apply a double gesture pointer to the navigation bar. But I was not successful, however, when the same gesture recognizer applied to this opinion, it works.

Here is the code I'm using:

// Create gesture recognizer, notice the selector method UITapGestureRecognizer *oneFingerTwoTaps = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease]; // Set required taps and number of touches [oneFingerTwoTaps setNumberOfTapsRequired:2]; [oneFingerTwoTaps setNumberOfTouchesRequired:1]; [self.view addGestureRecognizer:oneFingerTwoTaps]; 

This works in view mode, but when it is done:

 [self.navigationController.navigationBar addGestureRecognizer:oneFingerTwoTaps] 

does not work.

+4
source share
2 answers

To do this, you need to subclass UINavigationBar, redefine the init button in it and add your gesture recognizer there.

So, say that you are creating a subclass of "CustomNavigationBar" - in your m file you will have an init method like this:

 - (id)initWithCoder:(NSCoder *)aDecoder { if ((self = [super initWithCoder:aDecoder])) { UISwipeGestureRecognizer *swipeRight; swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipeRight:)]; [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight]; [swipeRight setNumberOfTouchesRequired:1]; [swipeRight setEnabled:YES]; [self addGestureRecognizer:swipeRight]; } return self; } 

Then you need to set the class name of your navigation bar in the interface builder for the name of your subclass.

enter image description here

It’s also convenient to add a delegate protocol to the navigation bar to listen for methods sent at the end of your gestures. For example - in the case of the above, swipe right:

 @protocol CustomNavigationbarDelegate <NSObject> - (void)customNavBarDidFinishSwipeRight; @end 

then in the m file - in the recognized gesture method (no matter what you do) you can call this delegate method.

Hope this helps

+4
source

For everyone who views this, this is a much simpler way to do this.

 [self.navigationController.view addGestureRecognizer:oneFingerTwoTaps]; 
+8
source

All Articles