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

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

假设是由于自己定义导航button而导致手势返回失效,那么能够在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时须要设置手势代理,不响应。

最新文章

  1. 中国CIO最关心的八大问题(上)
  2. 在AWS中创建NAT节点
  3. PHP输出控制(Output Control)函数
  4. 【Python】range和xrange区别
  5. php 基础语法
  6. virtualBox虚拟机安装与主机互访和实现上网配置
  7. EL四大作用域 9个jsp对象有效范围 及 对应的类
  8. 时间的获取和转换,C#和Sql
  9. leetcode Container With Most Water python
  10. 畅通工程续(Dijkstra算法)
  11. C# 反射、与dynamic最佳组合
  12. Qt Create or VS 2015 使用 Opencv330 相机静态库链接错误如何解决?
  13. SQL解决数值间隔问题
  14. 第三十九篇-RecyclerView的使用
  15. Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager 转发非原创
  16. day06-单表查询
  17. NFS 网络文件系统制作
  18. MySql+Memcached架构的问题
  19. HTML5学习--SVG全攻略(基础篇)
  20. selenium元素定位方法

热门文章

  1. HDU 4280 Island Transport
  2. zoj 2830 Champion of the Swordsmanship
  3. hdu2063 二分图匹配,匈牙利算法
  4. java中传入一个数或字符串或数组进行反转
  5. CSU-1336: Interesting Calculator,最短路思想!
  6. 初学划分树,小见解之!POJ-2104/HDU-2665
  7. [UOJ#219][BZOJ4650][Noi2016]优秀的拆分
  8. Uva5009 Error Curves
  9. K大数查询(bzoj 3110)
  10. 转 C++STL之string