iOS 7的手势滑动返回功能

Posted 「违规用户」

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 7的手势滑动返回功能相关的知识,希望对你有一定的参考价值。


隐藏navigationController的navigationBar后,系统自带的左边侧滑pop手势就消失了。

之前随手写过一篇《使用UIScreenEdgePanGestureRecognizer实现swipe to pop效果》,挺粗糙的。

现在使用默认模板创建的ios App都支持手势返回功能,如果导航栏的返回按钮是自定义的那么则会失效,也可以参考这里手动设置无效。

[cpp]  view plain  copy  
  1. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])   
  2.     self.navigationController.interactivePopGestureRecognizer.enabled = NO;  
  3.   

如果是因为自定义导航按钮而导致手势返回失效,那么可以在NavigationController的viewDidLoad函数中添加如下代码:

[cpp]  view plain  copy  
  1. - (void)viewDidLoad  
  2.   
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view.  
  5.       
  6.     __weak typeof (self) weakSelf = self;  
  7.     if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])   
  8.         self.interactivePopGestureRecognizer.delegate = weakSelf;  
  9.       
  10.   

这样写了以后就可以通过手势滑动返回上一层了,但是如果在push过程中触发手势滑动返回,会导致导航栏崩溃(从日志中可以看出)。针对这个问题,我们需要在pop过程禁用手势滑动返回功能:

[cpp]  view plain  copy  
  1. - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated  
  2.     
  3.     // fix 'nested pop animation can result in corrupted navigation bar'  
  4.     if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)])   
  5.         self.interactivePopGestureRecognizer.enabled = NO;  
  6.       
  7.       
  8.     [super pushViewController:viewController animated:animated];  
  9.   

[cpp]  view plain  copy  
  1. - (void)navigationController:(UINavigationController *)navigationController  
  2.        didShowViewController:(UIViewController *)viewController  
  3.                     animated:(BOOL)animated  
  4.   
  5.     if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])   
  6.         navigationController.interactivePopGestureRecognizer.enabled = YES;  
  7.       
  8.   

除了使用系统默认的动画,还可以使用自定义过渡动画( 丰满的文档):

[cpp]  view plain  copy  
  1. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController  
  2.                                   animationControllerForOperation:(UINavigationControllerOperation)operation  
  3.                                                fromViewController:(UIViewController *)fromVC  
  4.                                                  toViewController:(UIViewController *)toVC  
  5.   
  6.     if (operation == UINavigationControllerOperationPop)   
  7.         if (self.popAnimator == nil)   
  8.             self.popAnimator = [WQPopAnimator new];  
  9.           
  10.         return self.popAnimator;  
  11.       
  12.       
  13.     return nil;  
  14.   
  15.   
  16. - (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController  
  17.                          interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController  
  18.   
  19.     return self.popInteractionController;  
  20.   
  21.   
  22. #pragma mark -   
  23.   
  24. - (void)enablePanToPopForNavigationController:(UINavigationController *)navigationController  
  25.   
  26.     UIScreenEdgePanGestureRecognizer *left2rightSwipe = [[UIScreenEdgePanGestureRecognizer alloc]  
  27.                                                          initWithTarget:self  
  28.                                                          action:@selector(didPanToPop:)];  
  29.     //[left2rightSwipe setDelegate:self];  
  30.     [left2rightSwipe setEdges:UIRectEdgeLeft];  
  31.     [navigationController.view addGestureRecognizer:left2rightSwipe];  
  32.       
  33.     self.popAnimator = [WQPopAnimator new];  
  34.     self.supportPan2Pop = YES;  
  35.   
  36.   
  37. - (void)didPanToPop:(UIPanGestureRecognizer *)panGesture  
  38.   
  39.     if (!self.supportPan2Pop) return ;  
  40.       
  41.     UIView *view = self.navigationController.view;  
  42.       
  43.     if (panGesture.state == UIGestureRecognizerStateBegan)   
  44.         self.popInteractionController = [UIPercentDrivenInteractiveTransition new];  
  45.         [self.navigationController popViewControllerAnimated:YES];  
  46.      else if (panGesture.state == UIGestureRecognizerStateChanged)   
  47.         CGPoint translation = [panGesture translationInView:view];  
  48.         CGFloat d = fabs(translation.x / CGRectGetWidth(view.bounds));  
  49.         [self.popInteractionController updateInteractiveTransition:d];  
  50.      else if (panGesture.state == UIGestureRecognizerStateEnded)   
  51.         if ([panGesture velocityInView:view].x > 0)   
  52.             [self.popInteractionController finishInteractiveTransition];  
  53.          else   
  54.             [self.popInteractionController cancelInteractiveTransition];  
  55.           
  56.         self.popInteractionController = nil;  
  57.       
  58.   

如下这个代理方法是用来提供一个非交互式的过渡动画的:

[cpp]  view plain  copy  
  1. - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController  
  2.                                   animationControllerForOperation:(UINavigationControllerOperation)operation  
  3.                                                fromViewController:(UIViewController *)fromVC  
  4.                                                  toViewController:(UIViewController *)toVC  
  5.   
  6.     if (operation == UINavigationControllerOperationPop)   
  7.         if (self.popAnimator == nil)   
  8.             self.popAnimator = [WQPopAnimator new];  
  9.           
  10.         return self.popAnimator;  
  11.       
  12.       
  13.     return nil;  
  14. self.navigationItem.hide Back Button = YES 并在 iOS 7 上使用滑动手势返回

    使用 iOS 7 滑动导航时导航栏出现故障

    如何在 iOS 7 上的 UINavigationController 中禁用向后滑动手势

    解决右滑返回手势和UIScrollView中的手势冲突

    iOS手势(滑动)返回的实现(自定义返回按钮)

    带有自定义后退按钮的滑动手势冻结根视图控制器