使用uiswipegesturerecognizer默认功能无法多次弹出至ViewController

时间:2019-05-21 11:44:11

标签: ios swift uigesturerecognizer

您好,我正在使用Swift 4.0和Xcode 10.2。 我添加了以下代码来弹出先前的控制器。如果假设我一个接三个地推三个控制器,那么UISwipeGestureRecognizer在它不起作用之后只能弹出一次。我提供了以下代码详细信息。我被困在这一点上,没有得到任何相关的答案。

self.navigationController?.interactivePopGestureRecognizer!.isEnabled = true
self.navigationController?.interactivePopGestureRecognizer!.delegate = self
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

请给我解决方法。

1 个答案:

答案 0 :(得分:1)

我用3个控制器创建了与您相似的层次结构-VC1, VC2 and VC3

enter image description here

您可以按以下方式在控制器中处理interactivePopGestureRecognizer

  1. 设置interactivePopGestureRecognizer.delegate = self

  2. 实施gestureRecognizerShouldBegin(_:)方法以返回true / false以启用/禁用gesture

示例:

VC2中:interactivePopGestureRecognizerdisabled

VC3中:interactivePopGestureRecognizerenabled

class VC1: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

class VC2: UIViewController, UIGestureRecognizerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }

    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return false
    }
}

class VC3: UIViewController, UIGestureRecognizerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }

    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

无需每次都在不同的isEnabled中手动设置interactivePopGestureRecognizer的{​​{1}}属性,即删除以下代码,

controllers