再谈iOS 7的手势滑动返回功能

原创 2014年06月03日 20:00:13

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

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

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

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    __weak typeof (self) weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
    }
}

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

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{  
    // fix 'nested pop animation can result in corrupted navigation bar'
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    
    [super pushViewController:viewController animated:animated];
}

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated
{
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

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

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                  animationControllerForOperation:(UINavigationControllerOperation)operation
                                               fromViewController:(UIViewController *)fromVC
                                                 toViewController:(UIViewController *)toVC
{
    if (operation == UINavigationControllerOperationPop) {
        if (self.popAnimator == nil) {
            self.popAnimator = [WQPopAnimator new];
        }
        return self.popAnimator;
    }
    
    return nil;
}

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

#pragma mark - 

- (void)enablePanToPopForNavigationController:(UINavigationController *)navigationController
{
    UIScreenEdgePanGestureRecognizer *left2rightSwipe = [[UIScreenEdgePanGestureRecognizer alloc]
                                                         initWithTarget:self
                                                         action:@selector(didPanToPop:)];
    //[left2rightSwipe setDelegate:self];
    [left2rightSwipe setEdges:UIRectEdgeLeft];
    [navigationController.view addGestureRecognizer:left2rightSwipe];
    
    self.popAnimator = [WQPopAnimator new];
    self.supportPan2Pop = YES;
}

- (void)didPanToPop:(UIPanGestureRecognizer *)panGesture
{
    if (!self.supportPan2Pop) return ;
    
    UIView *view = self.navigationController.view;
    
    if (panGesture.state == UIGestureRecognizerStateBegan) {
        self.popInteractionController = [UIPercentDrivenInteractiveTransition new];
        [self.navigationController popViewControllerAnimated:YES];
    } else if (panGesture.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [panGesture translationInView:view];
        CGFloat d = fabs(translation.x / CGRectGetWidth(view.bounds));
        [self.popInteractionController updateInteractiveTransition:d];
    } else if (panGesture.state == UIGestureRecognizerStateEnded) {
        if ([panGesture velocityInView:view].x > 0) {
            [self.popInteractionController finishInteractiveTransition];
        } else {
            [self.popInteractionController cancelInteractiveTransition];
        }
        self.popInteractionController = nil;
    }
}

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

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                  animationControllerForOperation:(UINavigationControllerOperation)operation
                                               fromViewController:(UIViewController *)fromVC
                                                 toViewController:(UIViewController *)toVC
{
    if (operation == UINavigationControllerOperationPop) {
        if (self.popAnimator == nil) {
            self.popAnimator = [WQPopAnimator new];
        }
        return self.popAnimator;
    }
    
    return nil;
}

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

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

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

@interface WQPopAnimator : NSObject <UIViewControllerAnimatedTransitioning>

@end

#import "WQPopAnimator.h"

@implementation WQPopAnimator

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
    return 0.4;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    [[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view];
    
    __block CGRect toRect = toViewController.view.frame;
    CGFloat originX = toRect.origin.x;
    toRect.origin.x -= toRect.size.width / 3;
    toViewController.view.frame = toRect;
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        CGRect fromRect = fromViewController.view.frame;
        fromRect.origin.x += fromRect.size.width;
        fromViewController.view.frame = fromRect;
        
        toRect.origin.x = originX;
        toViewController.view.frame = toRect;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}

@end

其次,交互式动画是通过
UIPercentDrivenInteractiveTransition
来维护的,在滑动过程中根据滑动距离来进行更新:
} else if (panGesture.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [panGesture translationInView:view];
        CGFloat d = fabs(translation.x / CGRectGetWidth(view.bounds));
        [self.popInteractionController updateInteractiveTransition:d];
当手势结束时要做出收尾动作:

} else if (panGesture.state == UIGestureRecognizerStateEnded) {
        if ([panGesture velocityInView:view].x > 0) {
            [self.popInteractionController finishInteractiveTransition];
        } else {
            [self.popInteractionController cancelInteractiveTransition];
        }
        self.popInteractionController = nil;
    }

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

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated
{
    if (viewController == self.navigationController.pushingViewController) {
        self.supportPan2Pop = YES;
        self.navigationController.pushingViewController = nil;
    }

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

UIPercentDrivenInteractiveTransition Controller交互式转场切换动画

在iOS7以前,开发者如果希望定制导航控制器推入推出视图时的转场动画,一般都只能通过子类化UINavigationController或者自己编写动画代码去覆盖相应的方法,现在iOS7为开发者带来了福...
  • chengkaizone
  • chengkaizone
  • 2015年09月21日 06:37
  • 2434

uiviewcontroller 转场放大缩小动画

在iOS7以前,开发者如果希望定制导航控制器推入推出视图时的转场动画,一般都只能通过子类化UINavigationController或者自己编写动画代码去覆盖相应的方法,现在iOS7为开发者带来了福...
  • zhuzhihai1988
  • zhuzhihai1988
  • 2014年05月01日 00:56
  • 10313

iOS 自定义页面的切换动画与交互动画

  • 2014年08月11日 17:29
  • 41KB
  • 下载

iOS 边缘滑动返回

边缘滑动返回,在设置了自定义导航条或者自定义左button的时候会失效,通过这句话可以解决bug :self.navigationController.interactivePopGestureRe...
  • zhangjitao_boke
  • zhangjitao_boke
  • 2016年07月20日 16:56
  • 1695

iOS之手势滑动返回功能

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

iOS Objective-C 右滑返回手势

iOS导航栏的返回按钮一定制,系统的右滑返回手势就失效了,但是这么好的用户体验方式怎么可以没有呢。解决问题必须先找到原因,才能对症下药:原因:自定义返回按钮导致的该手势未起作用是因为在delegate...
  • fanxiaomeng92
  • fanxiaomeng92
  • 2016年10月24日 11:45
  • 1802

ios 手势右滑视觉差 效果类似微信QQ等返回操作

  • 2014年09月08日 10:07
  • 45KB
  • 下载

iOS7 interactive transitions(视图控制器的过渡动画 )

原文地址:http://dativestudios.com/blog/2013/09/29/interactive-transitions/ iOS7 引入了一种新的视图控制器过渡的方式,通过这篇...
  • yangxiyangyang
  • yangxiyangyang
  • 2015年03月27日 11:41
  • 944

IOS开发笔记——禁用手势滑动返回功能

在ios7以后,苹果推出了手势滑动返回功能,也就是从屏幕左侧向右滑动可依返回上一个界面。这项功能在大多数情况下方便了用户的使用,但是有时候,我们并不需要手势返回功能,比如某个页面加入了左右滑动翻页功能...
  • J362367731
  • J362367731
  • 2016年04月16日 13:42
  • 14815

IOS-63-禁用右滑返回的两种方式

方式一: 前提:如果使用的自定义UINavigationController基类,请不要在此基类里写相关的手势操作方法。 代码如下: -(void)viewDidAppear:(BOOL)ani...
  • IOT_LI
  • IOT_LI
  • 2016年06月29日 10:59
  • 6668
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:再谈iOS 7的手势滑动返回功能
举报原因:
原因补充:

(最多只允许输入30个字)