我知道如何简单地检测长按,但它是在释放后检测到的。如何在不松开手指的情况下检测到长按?
这是我现在用于长按的代码:
override func viewDidLoad() {
super.viewDidLoad()
setupLongPressGesture()
}
func setupLongPressGesture() {
let longPressGesture:UILongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPress))
longPressGesture.minimumPressDuration = 1.0 // 1 second press
longPressGesture.delegate = self
self.tableView.addGestureRecognizer(longPressGesture)
}
@objc func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer){
if gestureRecognizer.state == .ended {
let touchPoint = gestureRecognizer.location(in: self.tableView)
if let indexPath = tableView.indexPathForRow(at: touchPoint) {
}
}
}
最佳答案
将 .ended
更改为 .began
。
来自 UILongPressGestureRecognizer
的文档:
Long-press gestures are continuous. The gesture begins (
UIGestureRecognizer.State.began
) when the number of allowable fingers (numberOfTouchesRequired
) have been pressed for the specified period (minimumPressDuration
) and the touches do not move beyond the allowable range of movement (allowableMovement
). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizer.State.ended
) when any of the fingers are lifted.
关于ios - 检测长按tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52691960/