如何仅在一个视图控制器上专门禁用向后滑动

时间:2018-08-01 10:34:35

标签: swift uigesturerecognizer swipe

我知道有人问过类似的问题,但没有一个对我有用。

我有这段代码可以在项目中向后滑动

    class InteractivePopRecognizer: NSObject {

    // MARK: - Properties

    fileprivate weak var navigationController: UINavigationController?

    // MARK: - Init

    init(controller: UINavigationController) {
        self.navigationController = controller

        super.init()

        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }
}

extension InteractivePopRecognizer: UIGestureRecognizerDelegate {
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return (navigationController?.viewControllers.count ?? 0) > 1
    }

    // This is necessary because without it, subviews of your top controller can cancel out your gesture recognizer on the edge.
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

我有这个VC堆栈

HomescreenVC->登录/注册VC-> UserProfileVC

我不希望他们能够从UserProfileVC向后滑动。

2 个答案:

答案 0 :(得分:1)

一种更好的方法是在显示UserProfileVC

时从堆栈中清除它们
let profile  = self.storyboard?.instantiateViewController(withIdentifier: "profileID") as! UserProfileVC
self.navigationController?.viewControllers = [profile]

编辑:在profileVC中执行此操作

self.navigationController?.viewControllers = [self]

//

self.view.alpha = 0

UIView.animate(withDuration: 0.5) {

    self.view.alpha = 1

}

答案 1 :(得分:0)

我认为您也可以从那里删除手势识别器,原因就是您不尝试这样做。 尝试这样的事情:-

view.gestureRecognizers?.removeAll()