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