未调用 UILabel 子类中的 UIGestureRecognizer

UIGestureRecognizer in UILabel subclass not being called

我有一个 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 从未被调用。我做错了什么吗?

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

确保设置 self.userInteractionEnabled = true