LongPressGestureRecognizer

LongPressGestureRecognizer

我有一个UILabel的子类,如下所示:

class GestureLabel: UILabel {

    override init(frame: CGRect) {
        super.init(frame: frame)
        initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initialize()
    }

    func initialize() {
        self.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: Selector("longPressGestureRecognizer:")))
    }

    deinit {
        gestureRecognizers?.removeAll()
    }

    func longPressGestureRecognizer(sender: AnyObject) {
        print("this is never called")
    }
}

但从未调用过。我做错什么了吗?

最佳答案

默认情况下,userInteractionEnabledUILabels上被禁用,因此在添加手势识别器时必须手动启用它。
确保设置self.userInteractionEnabled = true

07-27 22:23