我有一个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
。我做错了吗?
答案 0 :(得分:6)
默认情况下userInteractionEnabled
已禁用UILabels
,因此您必须在添加手势识别器时手动启用它。
确保设置self.userInteractionEnabled = true