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

问题描述 投票:2回答:2

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

func longPressed(recognizer: UILongPressGestureRecognizer) {

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

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

enter image description here

控制台打印:

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

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

不久之后,这就是我期望的更少或更多:

location: \(32.0, 22.0) ...
ios swift uitableview uigesturerecognizer
2个回答
4
投票

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

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)

0
投票

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

   let location = gesture.location(in: tableView)
   let indexPath = tableView.indexPathForRow(at: location)
   Print(indexPath.row)
   Print(indexPath.section)
© www.soinside.com 2019 - 2024. All rights reserved.