NSPressGestureRecognizer 在 minimumPressDuration 之前调用
NSPressGestureRecognizer called before minimumPressDuration
我需要 NSButton
响应常规点击和长按。我像这样添加 NSPressGestureRecognizer
:
override func viewDidLoad() {
super.viewDidLoad()
let gr = NSPressGestureRecognizer()
gr.minimumPressDuration = 1
gr.action = #selector(handleLongPress)
button.addGestureRecognizer(gr)
}
func handleLongPress(gr: NSPressGestureRecognizer) {
if gr.state == .Began {
Swift.print("long press")
}
}
不幸的是,handleLongPress
即使在短暂的单击或双击时也会随机触发。即使我将 minimumPressDuration
设置为更高的值也会发生这种情况。
我试过 shouldBeRequiredToFailByGestureRecognizer
但它没有解决问题。
我的代码是否遗漏了什么?
我已经通过处理多个手势识别器解决了这个问题:
class 必须实施 NSGestureRecognizerDelegate
var singleClick:NSClickGestureRecognizer?
var longClick:NSPressGestureRecognizer?
override func viewDidLoad() {
super.viewDidLoad()
longClick = NSPressGestureRecognizer()
longClick!.minimumPressDuration = 1
longClick!.action = #selector(handleGestureLong)
longClick!.delegate = self
singleClick = NSClickGestureRecognizer()
singleClick?.numberOfClicksRequired = 1
singleClick!.action = #selector(handleGestureSingle)
singleClick!.delegate = self
btn.addGestureRecognizer(longClick!)
btn.addGestureRecognizer(singleClick!)
}
func gestureRecognizer(gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: NSGestureRecognizer) -> Bool {
if gestureRecognizer == singleClick && otherGestureRecognizer == longClick {
return false
}
return true
}
func handleGestureSingle(gr: NSClickGestureRecognizer) {
switch gr.state {
case .Ended:
print("single click")
break
default:
break
}
}
func handleGestureLong(gr: NSPressGestureRecognizer) {
switch gr.state {
case .Began:
print("long click")
break
default:
break
}
}
我需要 NSButton
响应常规点击和长按。我像这样添加 NSPressGestureRecognizer
:
override func viewDidLoad() {
super.viewDidLoad()
let gr = NSPressGestureRecognizer()
gr.minimumPressDuration = 1
gr.action = #selector(handleLongPress)
button.addGestureRecognizer(gr)
}
func handleLongPress(gr: NSPressGestureRecognizer) {
if gr.state == .Began {
Swift.print("long press")
}
}
不幸的是,handleLongPress
即使在短暂的单击或双击时也会随机触发。即使我将 minimumPressDuration
设置为更高的值也会发生这种情况。
我试过 shouldBeRequiredToFailByGestureRecognizer
但它没有解决问题。
我的代码是否遗漏了什么?
我已经通过处理多个手势识别器解决了这个问题:
class 必须实施 NSGestureRecognizerDelegate
var singleClick:NSClickGestureRecognizer?
var longClick:NSPressGestureRecognizer?
override func viewDidLoad() {
super.viewDidLoad()
longClick = NSPressGestureRecognizer()
longClick!.minimumPressDuration = 1
longClick!.action = #selector(handleGestureLong)
longClick!.delegate = self
singleClick = NSClickGestureRecognizer()
singleClick?.numberOfClicksRequired = 1
singleClick!.action = #selector(handleGestureSingle)
singleClick!.delegate = self
btn.addGestureRecognizer(longClick!)
btn.addGestureRecognizer(singleClick!)
}
func gestureRecognizer(gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOfGestureRecognizer otherGestureRecognizer: NSGestureRecognizer) -> Bool {
if gestureRecognizer == singleClick && otherGestureRecognizer == longClick {
return false
}
return true
}
func handleGestureSingle(gr: NSClickGestureRecognizer) {
switch gr.state {
case .Ended:
print("single click")
break
default:
break
}
}
func handleGestureLong(gr: NSPressGestureRecognizer) {
switch gr.state {
case .Began:
print("long click")
break
default:
break
}
}