美文网首页
iOS拦截返回事件解决方案

iOS拦截返回事件解决方案

作者: 霸哥终结者 | 来源:发表于2021-09-23 11:23 被阅读0次

    核心代码

    1. UINavigationController
    class NavigationController: UINavigationController, UINavigationControllerDelegate {
        
        var popDelegate: UIGestureRecognizerDelegate?
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            // 解决侧滑返回失效问题
            self.popDelegate = self.interactivePopGestureRecognizer?.delegate
            self.delegate = self
            
        }
        
    }
    
    // 如果你想使用的optional方法,你必须用@objc标记您的protocol
    public protocol ShouldPopDelegate {
        //拦截返回按钮的点击事件
        func currentViewControllerShouldPop() -> Bool
    }
    
    @objc extension UIViewController: ShouldPopDelegate {
        public func currentViewControllerShouldPop() -> Bool {
            return true
        }
    }
    
    extension NavigationController: UINavigationBarDelegate {
        public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool
        {
            if self.viewControllers.count < navigationBar.items?.count ?? 1{
                return true
            }
            var shouldPop = true
            // 看一下当前控制器有没有实现代理方法 currentViewControllerShouldPop
            // 如果实现了,根据当前控制器的代理方法的返回值决定
            // 没过没有实现 shouldPop = YES
            let currentVC = self.topViewController
            if (currentVC?.responds(to: #selector(currentViewControllerShouldPop)))! {
                shouldPop = (currentVC?.currentViewControllerShouldPop())!
            }
            
            if (shouldPop == true)
            {
                DispatchQueue.main.async {
                    self.popViewController(animated: true)
                }
                
            }
            else
            {
                // 让系统backIndicator 按钮透明度恢复为1
                for subview in navigationBar.subviews
                {
                    if (0.0 < subview.alpha && subview.alpha < 1.0) {
                        UIView.animate(withDuration: 0.25, animations: {
                            subview.alpha = 1.0
                        })
                    }
                }
                
            }
            return false
        }
        
        
    }
    
    
    1. 某个需要拦截的UIViewController
    class DemoViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    //        解决scrollView引起的侧滑返回冲突
    //        if let gesture = self.navigationController?.interactivePopGestureRecognizer {
    //            self.scrollView.panGestureRecognizer.require(toFail: gesture)
    //        }
        }
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            self.navigationController?.interactivePopGestureRecognizer?.delegate = self
            
        }
        
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            self.navigationController?.interactivePopGestureRecognizer?.delegate = (self.navigationController as? NavigationController)?.popDelegate
        }
    }
    
    extension DemoViewController: UINavigationBarDelegate, UIGestureRecognizerDelegate {
        // 侧滑返回
        func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
            return true
        }
        
        func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
            return true
        }
        // 按钮返回
        override func currentViewControllerShouldPop() -> Bool {
            return true
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS拦截返回事件解决方案

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