UIGestureRecognizer没有被释放

时间:2019-03-15 03:34:48

标签: ios swift memory-management uigesturerecognizer gesture-recognition

我有一个UIGestureRecognizer的子类,我将其添加到viewController.view中,然后当我调用removeGestureRecognizer时,它没有被释放,也不知道为什么。

我无法弄清楚谁仍在保留该实例,我没有任何实例变量或类似的东西保留它,只有viewController的视图和根据文档,在调用removeGestureRecognizer时,它应该释放手势,但不会。

希望有人可以帮助我解决这个问题。

ViewController

class ViewController: UIViewController, CustomGestureRecognizerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addGestureRecognizer(CustomGestureRecognizer(delegate: self))
    }

    func customGestureRecognizerDidDectectTouch(_ customGestureRecognizer: CustomGestureRecognizer) {
        print("customGestureRecognizerDidDectectTouch")
        self.view.removeGestureRecognizer(customGestureRecognizer)
    }
}

UIGestureRecognizer子类

protocol CustomGestureRecognizerDelegate: class {
    func customGestureRecognizerDidDectectTouch(_ customGestureRecognizer: CustomGestureRecognizer)
}

class CustomGestureRecognizer: UIGestureRecognizer {

    deinit {
        print("deinit CustomGestureRecognizer")
    }

    private weak var customDelegate: CustomGestureRecognizerDelegate?

    init(delegate: CustomGestureRecognizerDelegate) {
        self.customDelegate = delegate
        super.init(target: nil, action: nil)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
        touchDetected()
        super.touchesBegan(touches, with: event)
    }

    private func touchDetected() {
        customDelegate?.customGestureRecognizerDidDectectTouch(self)
    }
}

这只是代码的一部分,可能在真正的应用程序中完全没有用,但足以复制问题。

谢谢!

0 个答案:

没有答案