ios - 如果它们在表中,我如何使用 hitTest 获取底层 View ?

标签 ios swift xcode uitableview hittest

此代码帮助我获取主视图下方的 View :

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

         let location = touches.first!.locationInView(self.view)

         let view = self.view.hitTest(location, withEvent: event)

         print("View: %@", NSStringFromClass((view?.classForCoder)!))
}

有什么方法可以为一张 table 做到这一点吗?我有一个包含单元格的表格,我想查看用户正在拖动哪个单元格,或者单元格中的哪个 View 。我的目标是使用按钮禁用滚动,然后能够在表格上拖动并在用户拖动时打印出单元格或在单元格内查看。

最佳答案

您可以根据单元格 subview 查询 UITableView 以获取 index-path

let point = cellSubView.convertPoint(cellSubView.bounds.origin, toView: tableView)
let index = tableView.indexPathForRowAtPoint(point)

indexPathForRowAtPoint() 如果点在表中的任何行之外则返回 nil

编辑:要处理 tableView 上的触摸,您必须在其上设置手势识别器。

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapOnTableView(_:)))
tableView.addGestureRecognizer(tapGesture)

func didTapOnTableView(gesture: UITapGestureRecognizer){
    let touchPoint = gesture.locationInView(tableView)
    let index = tableView.indexPathForRowAtPoint(touchPoint)

    ...
}

关于ios - 如果它们在表中,我如何使用 hitTest 获取底层 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39282134/

相关文章:

ios - 如何根据设备方向 iOS 6 自动旋转 UIView?

xcode - 无法打开 Storyboard(com.apple.InterfaceBuilder 错误 -1。)

iphone - 使用 ios Camera 以方形形式聚焦到对象?

ios - 自定义颜色我的 UIActivityIndi​​catorView

ios - 如何使用 SQL 格式的动态 where 子句创建谓词

ios - Firebase 不在 M1 模拟器上构建(在范围内找不到 'Analytics')

ios - 隐藏另一个 View 时调整 View 大小

ios - 在 ios 中使用 Windows azure 移动服务发布数据时出现错误

swift - 可选 NSArray - Anyobject 没有下标成员

ios - 此 iPhone 5s 运行的是 iOS 11.0.3,此版本的 Xcode 可能不支持