ios - 如何在 UITableView 框架中获取手势识别器的位置,而不是边界

标签 ios swift uitableview uigesturerecognizer

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

最佳答案

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

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)

关于ios - 如何在 UITableView 框架中获取手势识别器的位置,而不是边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32646331/

相关文章:

objective-c - Swift 中的 _cmd_/__FUNCTION__

ios - 为什么 removeAllObservers() 在 deinit 中不起作用?

ios - 无法在Xcode6中设置UITableViewCell,因为“自动属性合成不会合成属性..”警告

java - PocketSocket 无法处理传入消息

objective-c - 调用 pop View Controller 方法时 iphone 应用程序崩溃

ios - 所选图像未按顺序显示

ios - 部分之间多个自定义 TableviewCell 上的多个自定义 UICollectionView

swift - UITableView CoreData 顺序与部分

更新 `isHidden`后iOS设置 `alpha`

iOS 在 ImageView 中交叉淡入淡出多个图像