4
votes

I have a PKRevealController with front and right views. Pan gesture is enabled, so I can open the right view with a swipe - great. Swiping another way doesn't do anything, as it shouldn't, because there's no left controller to open - great. So instead, I want to implement a swipe (left to right) that would trigger my app's back-navigation.

I've added a swipe gesture recognizer to my (front) view controller - nothing happens.

If I setRecognizesPanningOnFrontView:NO on the reveal controller, then my newly added gesture recognizer works - I can navigate back.

So, now it's either one or the other. I want both. How can I do that?

1

1 Answers

2
votes

I've implemented my own solution. Two solutions actually, as I needed different ones for cases when I allowed and disallowed PKRevealController swipe actions.

Case 1: setRecognizesPanningOnFrontView:NO

This case is quite easy - I simply add a UISwipeGestureRecognizer that calls my back method:

UISwipeGestureRecognizer *backSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(back)];
[backSwipe setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:backSwipe];

Case 2: setRecognizesPanningOnFrontView:YES

This one is slightly more complicated. In order to avoid gesture recogniser conflict, I had to tap into PKRevealController's gesture recogniser. Of course, I implemented this only when PKRevealController does not have a leftViewController.

So, I register the class which I want to implement the back swipe navigation as a listener to notifications:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(back:)
                                             name:NOTIFICATION_BACK_SWIPE
                                           object:self];

And then in PKRevealController.m file, in - (void)moveFrontViewRightwardsIfPossible method, simply post the notification:

[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_BACK_SWIPE object:[(UINavigationController *)self.frontViewController topViewController]];

Here I am passing as notification's object the recipient UIViewController itself. I'm doing this so that only that specific UIViewController instance reacts to this notification. Otherwise if you had more UIViewControllers in the UINavigationController stack that are subscribed to receive this notification, they would all cause the UINavigationController to popViewController, which would result in a random number of steps back while we want this to happen only once.

That's it. Enjoy.