UILabel子类中的UIGestureRecognizer未被调用

时间:2016-02-11 13:29:53

标签: ios swift uigesturerecognizer

我有一个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")
    }
}

但永远不会调用longPressGestureRecognizer。我做错了吗?

1 个答案:

答案 0 :(得分:6)

默认情况下userInteractionEnabled已禁用UILabels,因此您必须在添加手势识别器时手动启用它。

确保设置self.userInteractionEnabled = true