首页 > 代码库 > iOS 7的手势滑动返回

iOS 7的手势滑动返回

如今使用默认模板创建的iOS App都支持手势返回功能,假设导航栏的返回button是自己定义的那么则会失效,也能够參考这里手动设置无效。

[cpp] view plaincopy技术分享技术分享
  1. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {  
  2.     self.navigationController.interactivePopGestureRecognizer.enabled = NO;  
  3. }  

假设是由于自己定义导航button而导致手势返回失效,那么能够在NavigationController的viewDidLoad函数中加入例如以下代码:
[cpp] view plaincopy技术分享技术分享
  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 plaincopy技术分享技术分享
  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 plaincopy技术分享技术分享
  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 plaincopy技术分享技术分享
  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 plaincopy技术分享技术分享
  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. }  

而以下这个代理方法则是提供交互式动画:
[cpp] view plaincopy技术分享技术分享
  1. - (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController  
  2.                          interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController  
  3. {  
  4.     return self.popInteractionController;  
  5. }  

这两个组合起来使用。首先。我们须要有个动画:
[cpp] view plaincopy技术分享技术分享
  1. @interface WQPopAnimator : NSObject <UIViewControllerAnimatedTransitioning>  
  2.   
  3. @end  

[cpp] view plaincopy技术分享技术分享
  1. #import "WQPopAnimator.h"  
  2.   
  3. @implementation WQPopAnimator  
  4.   
  5. - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext  
  6. {  
  7.     return 0.4;  
  8. }  
  9.   
  10. - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext  
  11. {  
  12.     UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];  
  13.     UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];  
  14.     [[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view];  
  15.       
  16.     __block CGRect toRect = toViewController.view.frame;  
  17.     CGFloat originX = toRect.origin.x;  
  18.     toRect.origin.x -= toRect.size.width / 3;  
  19.     toViewController.view.frame = toRect;  
  20.       
  21.     [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{  
  22.         CGRect fromRect = fromViewController.view.frame;  
  23.         fromRect.origin.x += fromRect.size.width;  
  24.         fromViewController.view.frame = fromRect;  
  25.           
  26.         toRect.origin.x = originX;  
  27.         toViewController.view.frame = toRect;  
  28.     } completion:^(BOOL finished) {  
  29.         [transitionContext completeTransition:![transitionContext transitionWasCancelled]];  
  30.     }];  
  31. }  
  32.   
  33. @end  

其次。交互式动画是通过
[cpp] view plaincopy技术分享技术分享
  1. UIPercentDrivenInteractiveTransition  
来维护的。在滑动过程中依据滑动距离来进行更新:
[cpp] view plaincopy技术分享技术分享
  1. else if (panGesture.state == UIGestureRecognizerStateChanged) {  
  2.         CGPoint translation = [panGesture translationInView:view];  
  3.         CGFloat d = fabs(translation.x / CGRectGetWidth(view.bounds));  
  4.         [self.popInteractionController updateInteractiveTransition:d];  
当手势结束时要做出收尾动作:
[cpp] view plaincopy技术分享技术分享
  1. else if (panGesture.state == UIGestureRecognizerStateEnded) {  
  2.         if ([panGesture velocityInView:view].x > 0) {  
  3.             [self.popInteractionController finishInteractiveTransition];  
  4.         } else {  
  5.             [self.popInteractionController cancelInteractiveTransition];  
  6.         }  
  7.         self.popInteractionController = nil;  
  8.     }  

相同地。自己定义的动画也会有上面提到的导航栏崩溃问题。也能够通过类似的方法来解决:
[cpp] view plaincopy技术分享技术分享
  1. - (void)navigationController:(UINavigationController *)navigationController  
  2.        didShowViewController:(UIViewController *)viewController  
  3.                     animated:(BOOL)animated  
  4. {  
  5.     if (viewController == self.navigationController.pushingViewController) {  
  6.         self.supportPan2Pop = YES;  
  7.         self.navigationController.pushingViewController = nil;  
  8.     }  

补充:位于当前navgationController的第一个([0])viewController时须要设置手势代理,不响应。


iOS 7的手势滑动返回