ios侧滑返回:完美解决 interactivePopGestureRecognizer 卡住的问题
interactivePopGestureRecognizer
是iOS7推出的解决VeiwController
滑动后退的新功能,虽然很实用,但是坑也很多啊(比如在rootViewcontroller下,使用侧滑返回手势,可能就卡住了),这里给出如何完美解决interactivePopGestureRecognizer
卡住的问题.
当然我们要自定义UINavigationController来解决这个问题:
#import "MMNavController.h" @interface MMNavController () { } @end @implementation MMNavController - (id)initWithRootViewController:(UIViewController *)rootViewController { self = [super initWithRootViewController:rootViewController]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { __weak MMNavController *weakSelf = self; if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) { self.interactivePopGestureRecognizer.delegate = weakSelf; self.delegate = weakSelf; } } - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { if ( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] && animated == YES ) { self.interactivePopGestureRecognizer.enabled = NO; } [super pushViewController:viewController animated:animated]; } - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated { if ( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] && animated == YES ) { self.interactivePopGestureRecognizer.enabled = NO; } return [super popToRootViewControllerAnimated:animated]; } - (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated { if( [self respondsToSelector:@selector(interactivePopGestureRecognizer)] ) { self.interactivePopGestureRecognizer.enabled = NO; } return [super popToViewController:viewController animated:animated]; } #pragma mark UINavigationControllerDelegate - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animate { if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) { self.interactivePopGestureRecognizer.enabled = YES; } } -(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { if ( gestureRecognizer == self.interactivePopGestureRecognizer ) { if ( self.viewControllers.count < 2 || self.visibleViewController == [self.viewControllers objectAtIndex:0] ) { return NO; } } return YES; } @end