美文网首页
我的iOS开发笔记——右滑返回手势失效怎么办?

我的iOS开发笔记——右滑返回手势失效怎么办?

作者: TheRootNode | 来源:发表于2017-02-16 09:49 被阅读665次

    上篇文章中我记录了我个人比较常用的Mac终端命令,并且着重记录了一下cocoapods的安装和使用。最近,我在做项目的时候遇到右滑返回手势失效的问题,其实很在以前就碰到过类似的问题,那时候也通过查询资料顺利解决了。不过当再次碰到这个问题时,我脑袋里虽然有印象但是却一下子还原不出具体的解决方案了,所以决定在这里记录一下这个问题的解决方案。好记性不如烂笔头,动手写吧!

    右滑返回手势失效的原因

    无论是以前还是前些日子碰到的手势失效问题,原因都是因为自定义了左上角的返回按钮(自定义右上角的按钮不影响)。而自定义了返回按钮之后,会使返回手势代理被阻隔,从而无法正常调用返回的action,因而手势失效。

    解决方案

    1.重新给interactivePopGestureRecognizer.delegate设置代理,让被阻断的代理继续下去,从而调用系统的返回Action。
    2.如果知道了系统返回的Action方法名,我们就可以自定义一个手势,然后让这个自定义的手势执行系统的Action,从而实现右滑返回。

    方案1的具体实现:

    情况1:

    假设我们有两个viewController,分别是vc1和vc2。现在我们要从vc1通过导航栏push到vc2,而vc2的左上角的返回按钮是需要我们自定义的,这个时候我们可以在vc2里这么操作:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        //重新给interactivePopGestureRecognizer.delegate挂代理
        self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
        //必要时也可以加上这个
        //self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
        self.title = @"第二页";
        self.view.backgroundColor = [UIColor whiteColor];
        
        //自定义返回按钮
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 40, 40);
        [btn setTitle:@"返回" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(popTo:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithCustomView:btn];
        self.navigationItem.leftBarButtonItem = left;
    }
    

    如果页面比较少,这样做完全可以解决问题,但是如果页面很多,这样做显然费时费力,而且还容易少加,所以针对页面比较多的情况,可以使用如下方法:

    情况2:

    在页面交多的情况下,我们可以给所有属性相似的页面设置个公共的父类,这样我们只需要将一下代码放入父类实现即可。

        //重新给interactivePopGestureRecognizer.delegate挂代理
        self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
        //必要时也可以加上这个
        //self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    

    这种方法需要注意一个问题,如果在根页面设置了self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;,那么在push的时候可能造成卡顿,所以如果使用这种方法,那么最好是在父类里判断当前类是不是根视图,如果是根视图,那么久不重新挂代理。
    还有一种方式就是,创建一个公共的继承自UINavigationController的导航栏类,然后在该导航栏类里实现self.interactivePopGestureRecognizer.delegate = (id)self;。如果用这种方式,你的导航容器需要使用这个自建的类。比如,创建了一个叫做BaseNavViewController的导航控制器,如下:

    .h文件
    #import <UIKit/UIKit.h>
    
    @interface BaseNavViewController : UINavigationController
    
    @end
    
    .m文件
    #import "BaseNavViewController.h"
    
    @interface BaseNavViewController ()
    
    @end
    
    @implementation BaseNavViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    

    那么如果你的导航控制器就必须使用它(用storybored道理也一样):

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        
        ViewController *vc = [[ViewController alloc] init];
        BaseNavViewController *nv = [[BaseNavViewController alloc] initWithRootViewController:vc];
        
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.window.rootViewController = nv;
        [self.window makeKeyAndVisible];
        
        return YES;
    }
    

    之后,只需要在BaseNavViewController里实现

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.interactivePopGestureRecognizer.delegate = (id)self;
    }
    

    也可以解决手势返回失效的问题。

    方案2具体实现

    我们先看一下在UIVIewcongtroller中self.navigationController.interactivePopGestureRecognizer里有哪些东西:

    (lldb) po self.navigationController.interactivePopGestureRecognizer
    <UIScreenEdgePanGestureRecognizer: 0x7fac4ee11e80; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7fac4ec097c0>; target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7fac4ee11d40>)>>
    

    在控制台中打印self.navigationController.interactivePopGestureRecognizer可以得到上面这些信息。这里我们需要关注的是target= <(action=handleNavigationTransition:, target=<_UINavigationInteractiveTransition 0x7fac4ee11d40>)>这一部分的信息,很显然这里包含了系统调用的action以及系统调用action时的target。所以,我们只要把这部分的信息拿出来然后让自定义的pan手势实现它就可以实现右滑返回的效果了,如下:

    NSArray *targets = [self.navigationController.interactivePopGestureRecognizer valueForKey:@"targets"];
    id target = [targets.firstObject valueForKey:@"target"];
    SEL action = NSSelectorFromString(@"handleNavigationTransition:");
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:action];
    [self.view addGestureRecognizer:pan];
    

    这样,我们就可以通过自定义的pan手势来执行系统的action打到实现右滑返回的效果,而且还是全屏右滑返回。
    以上就介绍了两种可以解决右滑返回失效的方案,并且第二种方案只是这一思想的简单实现,网上也有大神给出了更细致的实现,传送门

    相关文章

      网友评论

          本文标题:我的iOS开发笔记——右滑返回手势失效怎么办?

          本文链接:https://www.haomeiwen.com/subject/lkeqwttx.html