美文网首页
Swift 导航全屏滑动返回实现

Swift 导航全屏滑动返回实现

作者: 蹦擦擦我勒个去 | 来源:发表于2019-01-09 16:42 被阅读0次
    import UIKit
    
    fileprivate var slideEnabledKey = "slideEnabled"
    
    extension UINavigationController {
        /// should slide to back. default is true
        var slideEnabled: Bool {
            set {
                objc_setAssociatedObject(self, &slideEnabledKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
            }
            get {
                if let rs = objc_getAssociatedObject(self, &slideEnabledKey) as? Bool {
                    return rs
                }
                return true
            }
        }
    }
    
    /// the navigationController enabled full screen slide to back
    class FullNavigationController: UINavigationController, UIGestureRecognizerDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            fullSlide()
        }
    
        func fullSlide() {
            guard let recognizer = interactivePopGestureRecognizer else {
                return
            }
            guard let recognizerView = recognizer.view else {
                return
            }
    
            // use runtime to check the property name
    //        var count: UInt32 = 0
    //        let ivars = class_copyIvarList(UIGestureRecognizer.self, &count)!
    //        for i in 0..<count {
    //            let ivar = ivars[Int(i)]
    //            let name = ivar_getName(ivar)
    //            print(String(cString: name!))
    //        }
    
            let targets = recognizer.value(forKey: "_targets") as? [NSObject]
            guard let targetObjc = targets?.first else {
                return
            }
    
            guard let target = targetObjc.value(forKey: "target") else {
                return
            }
            let action = Selector(("handleNavigationTransition:"))
            let fullRecognizer = UIPanGestureRecognizer()
            fullRecognizer.delegate = self
            recognizerView.addGestureRecognizer(fullRecognizer)
            fullRecognizer.addTarget(target, action: action)
        }
    
        override func pushViewController(_ viewController: UIViewController, animated: Bool) {
            if viewControllers.count > 0 {
                viewController.hidesBottomBarWhenPushed = true
    
            }
            super.pushViewController(viewController, animated: animated)
        }
    
        func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
            return slideEnabled && childViewControllers.count > 1
        }
    }
    

    相关文章

      网友评论

          本文标题:Swift 导航全屏滑动返回实现

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