Ios 如何从长按开始检测抽头?
我想检测3个动作,“点击开始”,“长按开始”,“长按结束”。我想检测“点击开始”,无论检测长点击(即每次触摸屏幕时,检测“点击开始”),并检测“点击开始”,然后检测“长按开始”,以防继续触摸 只有在未检测到“长抽头”的情况下,以下代码才能检测到“抽头开始”Ios 如何从长按开始检测抽头?,ios,swift,uitapgesturerecognizer,Ios,Swift,Uitapgesturerecognizer,我想检测3个动作,“点击开始”,“长按开始”,“长按结束”。我想检测“点击开始”,无论检测长点击(即每次触摸屏幕时,检测“点击开始”),并检测“点击开始”,然后检测“长按开始”,以防继续触摸 只有在未检测到“长抽头”的情况下,以下代码才能检测到“抽头开始” let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.Long(_:))) longPressGest
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.Long(_:)))
longPressGesture.minimumPressDuration = 3
longPressGesture.allowableMovement = 30
let shortPressGesture = UITapGestureRecognizer(target: self, action: #selector(self.Tap(_:)))
touchView.addGestureRecognizer(shortPressGesture)
touchView.addGestureRecognizer(longPressGesture)
}
@objc func Long(_ sender: UILongPressGestureRecognizer) {
if(sender.state == UIGestureRecognizer.State.began) {
print("Long tap begin")
} else if (sender.state == UIGestureRecognizer.State.ended) {
print("Long tap ended")
}
}
@objc func Tap(_ sender: UITapGestureRecognizer) {
print("Tap begin")
}
您需要遵守
UIgestureRecognitizerDelegate
class ViewController: UIViewController, UIGestureRecognizerDelegate
然后使用功能同时执行should recognize,以允许两个手势识别器同时工作
此外,我认为您实际上想要使用两个UILongPressGetSutureRecogeners,因为在润色时会检测到轻触
@objc func Long(_ sender: UILongPressGestureRecognizer) {
if(sender.state == UIGestureRecognizer.State.began) {
print("Long tap begin")
} else if (sender.state == UIGestureRecognizer.State.ended) {
print("Long tap ended")
}
}
@objc func Tap(_ sender: UILongPressGestureRecognizer) {
if(sender.state == UIGestureRecognizer.State.began) {
print("Tap begin")
} else if (sender.state == UIGestureRecognizer.State.ended) {
print("Tap ended")
}
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == longPressGesture && otherGestureRecognizer == shortPressGesture {
return true
}
return false
}
最后,不要忘记将手势识别器的代理设置为self
tapPressGesture.delegate = self
shortPressGesture.delegate = self
你不能在印刷时启动计时器,在发行时评估它吗?