All Projects → wangrui460 → BackBtnEventIntercept_swift

wangrui460 / BackBtnEventIntercept_swift

Licence: MIT License
系统返回按钮事件拦截 System return button event interception

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to BackBtnEventIntercept swift

penguins-eggs
On the road of Remastersys, Refracta, Systemback and father Knoppix!
Stars: ✭ 38 (+171.43%)
Mutual labels:  systemback

iOS 技术交流

我创建了一个 微信 iOS 技术交流群,欢迎小伙伴们加入一起交流学习~

可以加我微信我拉你进去(备注iOS),我的微信号 wr1204607318

BackBtnEventIntercept_swift

系统返回按钮事件拦截 OC 版本

  • 主要实现原理

// 如果你想使用的optional方法,你必须用@objc标记您的protocol
public protocol ShouldPopDelegate
{
    func currentViewControllerShouldPop() -> Bool
}

extension UIViewController: ShouldPopDelegate
{
    public func currentViewControllerShouldPop() -> Bool {
        return true
    }
}

extension UINavigationController: UINavigationBarDelegate
{
    public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool
    {
        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)
            }
            // 这里要return, 否则这个方法将会被再次调用
            return 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
        }
    }
}
  • 如何使用

// 如果需要拦截系统返回按钮就重写该方法返回 false
override func currentViewControllerShouldPop() -> Bool {
    return false
}
  • 如何禁用系统👉右滑返回手势

override func viewWillAppear(_ animated: Bool) 
{
    super.viewWillAppear(animated)
    // 为当前控制器禁用👉右滑返回手势
    if (navigationController?.responds(to: NSSelectorFromString("interactivePopGestureRecognizer")))! {
        navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    }
}

override func viewWillDisappear(_ animated: Bool) 
{
    super.viewWillDisappear(animated)
    // 为其他控制器开启👉右滑返回手势
    if (navigationController?.responds(to: NSSelectorFromString("interactivePopGestureRecognizer")))! {
        navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
}

联系我

扫码回复1获取面试资料(持续更新)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].