美文网首页goodUI很常
iOS 单个页面禁用侧滑返回(禁用手势退出)

iOS 单个页面禁用侧滑返回(禁用手势退出)

作者: Jesscia_Liu | 来源:发表于2021-05-10 16:47 被阅读0次

    方法一 OC:

    - (void)viewDidLoad {
        [super viewDidLoad];
        id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
        UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
        [self.view addGestureRecognizer:pan];
    }
    

    方法二 Swift:

      override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            UIApplication.shared.isIdleTimerDisabled = true
            self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false;
        }
    
        override func viewDidDisappear(_ animated: Bool) {
            super.viewDidDisappear(animated)
            UIApplication.shared.isIdleTimerDisabled = false
            self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true;
        }
    

    方法三:

    • 先写一个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;
            }
        }
    }
    
    
    • 再在控制器里使用
    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        [UIViewController popGestureClose:self];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        [UIViewController popGestureOpen:self];
    }
    

    参考

    iOS 禁止某个页面侧滑返回
    iOS 禁用右滑返回手势
    iOS右滑返回手势深度全解和最佳实施方案

    相关文章

      网友评论

        本文标题:iOS 单个页面禁用侧滑返回(禁用手势退出)

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