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

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

  1. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
  2. self.navigationController.interactivePopGestureRecognizer.enabled = NO;
  3. }

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

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

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

  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. [super pushViewController:viewController animated:animated];
  8. }
  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. }

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

  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. return nil;
  13. }
  14. - (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
  15. interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
  16. {
  17. return self.popInteractionController;
  18. }
  19. #pragma mark -
  20. - (void)enablePanToPopForNavigationController:(UINavigationController *)navigationController
  21. {
  22. UIScreenEdgePanGestureRecognizer *left2rightSwipe = [[UIScreenEdgePanGestureRecognizer alloc]
  23. initWithTarget:self
  24. action:@selector(didPanToPop:)];
  25. //[left2rightSwipe setDelegate:self];
  26. [left2rightSwipe setEdges:UIRectEdgeLeft];
  27. [navigationController.view addGestureRecognizer:left2rightSwipe];
  28. self.popAnimator = [WQPopAnimator new];
  29. self.supportPan2Pop = YES;
  30. }
  31. - (void)didPanToPop:(UIPanGestureRecognizer *)panGesture
  32. {
  33. if (!self.supportPan2Pop) return ;
  34. UIView *view = self.navigationController.view;
  35. if (panGesture.state == UIGestureRecognizerStateBegan) {
  36. self.popInteractionController = [UIPercentDrivenInteractiveTransition new];
  37. [self.navigationController popViewControllerAnimated:YES];
  38. } else if (panGesture.state == UIGestureRecognizerStateChanged) {
  39. CGPoint translation = [panGesture translationInView:view];
  40. CGFloat d = fabs(translation.x / CGRectGetWidth(view.bounds));
  41. [self.popInteractionController updateInteractiveTransition:d];
  42. } else if (panGesture.state == UIGestureRecognizerStateEnded) {
  43. if ([panGesture velocityInView:view].x > 0) {
  44. [self.popInteractionController finishInteractiveTransition];
  45. } else {
  46. [self.popInteractionController cancelInteractiveTransition];
  47. }
  48. self.popInteractionController = nil;
  49. }
  50. }

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

  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. return nil;
  13. }

而下面这个代理方法则是提供交互式动画:

  1. - (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
  2. interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
  3. {
  4. return self.popInteractionController;
  5. }

这两个组合起来使用。首先,我们需要有个动画:

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

其次,交互式动画是通过

  1. UIPercentDrivenInteractiveTransition

来维护的,在滑动过程中根据滑动距离来进行更新:

  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];

当手势结束时要做出收尾动作:

  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. }

同样地,自定义的动画也会有上面提到的导航栏崩溃问题,也可以通过类似的方法来解决:

  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的手势滑动返回功能的更多相关文章

  1. iOS 7的手势滑动返回

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

  2. 禁用ios7 手势滑动返回功能

    禁用ios7 手势滑动返回功能 版权声明:本文为博主原创文章,未经博主允许不得转载. 在有的时候,我们不需要手势返回功能,那么可以在页面中添加以下代码: - (void)viewDidAppear:( ...

  3. iOS之手势滑动返回功能-b

    iOS中如果不自定义UINavigationBar,通过手势向右滑是可以实现返回的,这时左边的标题文字提示的是上一个ViewController的标题,如果需要把文字改为简约风格,例如弄过箭头返回啥的 ...

  4. iOS之手势滑动返回功能

    iOS中如果不自定义UINavigationBar,通过手势向右滑是可以实现返回的,这时左边的标题文字提示的是上一个ViewController的标题,如果需要把文字改为简约风格,例如弄过箭头返回啥的 ...

  5. iOS 应用全部添加滑动返回

    if ([self  class] == [HomeViewController class]||[self  class] == [ComprehensivefinanceViewControlle ...

  6. Android-通过SlidingMenu高仿微信6.2最新版手势滑动返回(二)

    转载请标明出处: http://blog.csdn.net/hanhailong726188/article/details/46453627 本文出自:[海龙的博客] 一.概述 在上一篇博文中,博文 ...

  7. Android-通过SlidingPaneLayout高仿微信6.2最新版手势滑动返回(一)

    近期更新了微信版本号到6.2.发现里面有个很好的体验,就是在第二个页面Activity能手势向右滑动返回,在手势滑动的过程中能看到第一个页面,这样的体验很赞,这里高仿了一下. 这里使用的是v4包里面的 ...

  8. iOS彩票项目--第五天,新特性引导页的封装、返回按钮的自定义、导航控制器的滑动返回以及自定义滑动返回功能

    一.上次实现了在AppDelegate中通过判断app版本决定是否进入新特性页面,今天将AppDelegate中的一坨进行了封装.将self.window的根控制器到底应该为新特性界面,还是主页面,封 ...

  9. UINavigationController实现全屏滑动返回功能

    说明: UINavigationController默认在push出的控制器中都有边沿滑动返回功能,但是只能从屏幕左边滑才能返回,若从屏幕中间画并没有效果.下面实现全屏滑动功能. 探究: 系统默认能够 ...

随机推荐

  1. 【Linux】替换文本中的字符

    替换单个文本中的字符,有两种方法,如下详解 VIM替换 打开文件 vim test.txt 替换 :%s/原字符串/替换字符串/gg 直接替换 sed -i 's/原字符串/替换字符串/g' `ls ...

  2. SchemaExport

    不在xml中配置         <!-- Drop and re-create the database schema on startup         <property name ...

  3. Windows 2008 R2有效激活方法【转】

    原文地址:http://jingyan.baidu.com/article/851fbc3707096e3e1f15ab33.html 装了windows server 2008之后才发现,它与win ...

  4. 萌新学习Python爬取B站弹幕+R语言分词demo说明

    代码地址如下:http://www.demodashi.com/demo/11578.html 一.写在前面 之前在简书首页看到了Python爬虫的介绍,于是就想着爬取B站弹幕并绘制词云,因此有了这样 ...

  5. unity3d的三种平面坐标系

    unity3d有如下三种平面坐标系: 1.屏幕坐标系 2.视口坐标系viewport 3.GUI坐标系

  6. Cocos2d-X中的ZORDER和Tag

    ZORDER:是描写叙述渲染顺序的值,每一个CCNode都有ZORDER,默认是0 ZORDER越大,越后面绘制 假设ZORDER同样.那么看arrival顺序.先增加的节点先绘制 ZORDER仅仅在 ...

  7. 英文版windows乱码问题(win7/8/10)

  8. atitit.md5算法的原理 与 总结

    atitit.md5算法的原理 与 总结 1. MD5的位数 128位1 2. 字节数组转换为32位字符串 base161 2.1. 十六进制字符用4个二进制位来表示1 2.2. byte[]和十六进 ...

  9. JDBC深度封装的工具类 (具有高度可重用性)

    首先介绍一下Dbutils:    Common Dbutils是操作数据库的组件,对传统操作数据库的类进行二次封装,可以把结果集转化成List. 补充一下,传统操作数据库的类指的是JDBC(java ...

  10. Java接口的异常设计

    一.问题的提出   疑惑1:在设计接口的时,对于接口方法何时需要声明抛出受检异常或者说所有的接口方法最后都声明抛出受检异常? 以下是代码片段: public interface xx{ public ...