ios - 检测长按tableview

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

        }
    }
}

最佳答案

.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/

相关文章:

ios - CAPS 页面菜单 : willMoveToPage delegate method getting error

html - '背景大小 :cover' does not work in ipad

ios - 如何在动画期间使按钮处于非事件状态

string - Swift 是否支持字符串 block ?

ios - 如何使用 dequeueReusableCellWithIdentifier 快速管理 UITableView 中的购物车?

iphone - 旋转和裁剪图像IOS

ios - 调整导航 Controller 大小仅适用于纵向?

ios - 如何从主视图中 ScrollView 内的 subview 中关闭 IOS Swift 中的键盘?

iOS:如何在屏幕上对控件进行分组,例如在“设置”中?

iphone - 如何同时从 UITableView 插入和删除一行