美文网首页iOS 开发小集
iOS 禁用右滑返回手势

iOS 禁用右滑返回手势

作者: ChancePro | 来源:发表于2019-02-26 16:13 被阅读0次

    可以通过设置页面的VC.navigationController.interactivePopGestureRecognizer.enabled来控制当前页面的右滑返回手势是否可用。可以创建一个UIViewController 的分类创建两个类方法。

    + (void)popGestureClose:(UIViewController *)VC
    {
        // 禁用侧滑返回手势
        if ([VC.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            //这里对添加到右滑视图上的所有手势禁用
            for (UIGestureRecognizer *popGesture in VC.navigationController.interactivePopGestureRecognizer.view.gestureRecognizers) {
                popGesture.enabled = NO;
            }
        }
    }
    
    + (void)popGestureOpen:(UIViewController *)VC
    {
        // 启用侧滑返回手势
        if ([VC.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            //这里对添加到右滑视图上的所有手势启用
            for (UIGestureRecognizer *popGesture in VC.navigationController.interactivePopGestureRecognizer.view.gestureRecognizers) {
                popGesture.enabled = YES;
            }
        }
    }
    

    viewDidAppear:中禁用手势,viewWillDisappear:中开启手势。

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        [UIViewController popGestureClose:self];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        [UIViewController popGestureOpen:self];
    }
    

    参考链接
    iOS右滑返回手势深度全解和最佳实施方案

    相关文章

      网友评论

        本文标题:iOS 禁用右滑返回手势

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