注: 它这种方法写的貌似不全面,如果多次快速操作push pop操作后可能会导致页面卡顿的严重Bug,可看下一篇文章,那个暂时未发现问题
转载自:http://www.devdiv.com/custom_backbarbuttonitem_interactivepopgesturerecognizer_-blog-37464-52866.html
WLNavigationControlller : UINavigationController <UIGestureRecognizerDelegate,UINavigationControllerDelegate>
{
[super viewDidLoad];
// Do any additional setup after loading the view.
__weak WLNavigationControlller *weakSelf = self;
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
{
self.interactivePopGestureRecognizer.delegate = weakSelf;
self.delegate = weakSelf;
}
}
步骤三:
重构pushViewController方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
self.interactivePopGestureRecognizer.enabled = NO;
[super pushViewController:viewController animated:animated];
}
步骤四:
实现delegate方法
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animate
{
// Enable the gesture again once the new controller is shown
if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])
self.interactivePopGestureRecognizer.enabled = YES;
}
如此四步,就可以解决手势返回的问题,且不会出现freeze住UI的bug。
我分析认为UIViewController应该实现了UIGestureRecongnizer的target action,并实现了delegate方法,当人为修改了BackBarButtonItem属性以后,interactivePopGestureRecognizer的delegate为nil,导致手势功能失效(我认为这里是通过delegate来实现pop功能),所以在viewDidLoad里要给interactivePopGestureRecognizer设置delegate属性,但并不用具体实现delegate方法。在push和push成功以后设置enabled属性我猜想是因为在手势的过程中一些事件没处理好,导致页面无法响应事件,出现假死的状况。不知到这个是不是苹果的bug。