zoukankan      html  css  js  c++  java
  • 再谈iOS 7的手势滑动返回功能

    本文转载至 http://blog.csdn.net/jasonblog/article/details/28282147 
    之前随手写过一篇《使用UIScreenEdgePanGestureRecognizer实现swipe to pop效果》,挺粗糙的。

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

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {  
    2.     self.navigationController.interactivePopGestureRecognizer.enabled = NO;  
    3. }  

    如果是因为自定义导航按钮而导致手势返回失效,那么可以在NavigationController的viewDidLoad函数中添加如下代码:
    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    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在CODE上查看代码片派生到我的代码片
     
    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在CODE上查看代码片派生到我的代码片
     
    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在CODE上查看代码片派生到我的代码片
     
    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在CODE上查看代码片派生到我的代码片
     
    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在CODE上查看代码片派生到我的代码片
     
    1. - (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController  
    2.                          interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController  
    3. {  
    4.     return self.popInteractionController;  
    5. }  

    这两个组合起来使用。首先,我们需要有个动画:
    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. @interface WQPopAnimator : NSObject <UIViewControllerAnimatedTransitioning>  
    2.   
    3. @end  

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    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在CODE上查看代码片派生到我的代码片
     
    1. UIPercentDrivenInteractiveTransition  
    来维护的,在滑动过程中根据滑动距离来进行更新:
    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    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在CODE上查看代码片派生到我的代码片
     
    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在CODE上查看代码片派生到我的代码片
     
    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时需要设置手势代理,不响应。

  • 相关阅读:
    返回一个首尾相连的整数数组中最大子数组的和
    《程序员修炼之道——从小工到专家》读后感一
    成功之路,贵在坚持
    有些路走下去会很苦很累,但是不走会后悔
    商品进行倒计时
    Hibernate中HQL函数汇总及获取当前时间进行比较举例
    java类的继承,多态,抽象类与接口
    equals()与 == 比较,hashCode方法
    面向对象基本知识
    命令控制台与java环境配置
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/4425558.html
Copyright © 2011-2022 走看看