如何在 UITableView 的框架中获取 gestureRecognizer 的位置,而不是边界

How to get location of gestureRecognizer in UITableView's frame, not bounds

在我的 UITableView 子类中,我有一个在识别长按手势时调用的方法:

func longPressed(recognizer: UILongPressGestureRecognizer) {

    let location = recognizer.locationInView(self)
    print("location: \(location), \(frame.origin), \(bounds.origin)")
}

在这种情况下(红色圆圈是我点击的地方):

控制台打印:

location: (32.0, 277.5), (0.0, 50.0), (0.0, 261.5)

所以我的位置在边界坐标内?我需要 UITableView 中的绝对触摸位置,但不包括滚动。可能吗?

很快这就是我对这里或多或少的期望:

location: \(32.0, 22.0) ...

视图的框架在父视图坐标系中指定。所以你可以使用:

let locationInSuperView = recognizer.locationInView(self.superview)

或者您可以自己删除滚动偏移量:

let contentOffset = self.contentOffset
let location = recognizer.locationInView(self)
let locationWithoutScroll = CGPoint( x: location.x - contentOffset.x,
                                     y: location.y - contentOffset.y)

获取 tableView 在特定单元格上的单元格位置。

   let location = gesture.location(in: tableView)
   let indexPath = tableView.indexPathForRow(at: location)
   Print(indexPath.row)
   Print(indexPath.section)