在我的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)