如果它们在 table 中,我如何使用 hitTest 获取底层视图?

How can I use hitTest to get underlying views if they are in a table?

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

    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 做到这一点吗?我有一个带有单元格的 table,我想查看用户拖过哪个单元格,或者单元格中的哪个视图。我的目标是使用一个按钮来禁用滚动,然后能够在 table 上拖动并在用户拖动它们时打印出单元格或单元格内的视图。

您可以根据单元格子视图在 UITableView 中查询 index-path

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

indexPathForRowAtPoint() returns 如果点在 table

中的任何行之外则为零

编辑:要处理 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)

    ...
}