我已经实现了自己的解决方案。实际上,我需要两个解决方案,因为当我允许和禁止
PKRevealController
的滑动操作时需要不同的解决方案。
情况1:
setRecognizesPanningOnFrontView:NO
这种情况非常简单 - 我只需添加一个
UISwipeGestureRecognizer
调用我的
back
方法:
UISwipeGestureRecognizer *backSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(back)]
[backSwipe setDirection:UISwipeGestureRecognizerDirectionRight]
[self.view addGestureRecognizer:backSwipe]
案例2: setRecognizesPanningOnFrontView:YES
这个稍微有点复杂。为了避免手势识别冲突,我不得不利用PKRevealController
的手势识别器。当然,只有在PKRevealController
没有leftViewController
时才实现这一点。
因此,我将想要实现后滑导航的类注册为通知的监听器:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(back:)
name:NOTIFICATION_BACK_SWIPE
object:self]
然后在PKRevealController.m文件中,在- (void)moveFrontViewRightwardsIfPossible
方法中,只需发布通知:
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_BACK_SWIPE object:[(UINavigationController *)self.frontViewController topViewController]]
在这里,我将 UIViewController
本身作为通知对象传递。我这样做是为了只让特定的 UIViewController
实例对此通知做出反应。否则,如果您在 UINavigationController
堆栈中有更多订阅接收此通知的 UIViewControllers
,它们都会导致 UINavigationController
弹出视图控制器 (popViewController
),这将导致向后随机步数,而我们希望这仅发生一次。
就是这样。享受吧。