拦截滑动手势swift写法

/// 先记录个草稿

@objc protocol SNBackButtonHandler {
    @objc optional func navigationPopOnBackButton() -> Bool
}

self.delegate = self
interactivePopGestureRecognizer?.delegate = self


extension UINavigationController: UINavigationBarDelegate, UIGestureRecognizerDelegate {

    public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
        if self.viewControllers.count < navigationBar.items!.count {
            return false
        }
        if let topVC = self.topViewController {
            if topVC.responds(to: #selector(navigationPopOnBackButton)) {
                return topVC.navigationPopOnBackButton()
            }
        }
        return true
    }

    public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == self.interactivePopGestureRecognizer {
            if self.viewControllers.count <= 1 {
                return false
            }
            if let topVC = self.topViewController {
                if topVC.responds(to: #selector(navigationPopOnBackButton)) {
                    return topVC.navigationPopOnBackButton()
                }
            }
        }
        return true
    }
}

extension UIViewController: SNBackButtonHandler {
    func navigationPopOnBackButton() -> Bool {
        return true
    }
}

你可能感兴趣的:(拦截滑动手势swift写法)