检测长按表格视图

时间:2018-10-07 19:13:14

标签: ios swift uitableview uilongpressgesturerecogni

我知道如何简单地检测长按,但是释放后会检测到。如何在不松开手指的情况下检测长按?

这是我现在长按的代码:

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) {

        }
    }
}

1 个答案:

答案 0 :(得分:1)

.ended更改为.began

摘自UILongPressGestureRecognizer的文档:

  

长按手势是连续的。当在指定时间段(UIGestureRecognizer.State.began中按下允许的手指数(numberOfTouchesRequired)且触摸不会超出允许的移动范围( minimumPressDuration)。每当手指移动时,手势识别器都会转换为“更改”状态,并且在任何手指抬起时手势识别器都会结束(allowableMovement