将 TableView 点击位置转换为单元格视图位置
Transform TableView Tap Location to Cell View Location
我的 TableView 上有一个 TapGestureRecognizer。
它让我找到了我点击过的手机。
接下来我想使用点击的位置来检查单元格上的标签是否被点击。
但当然我有相对于 TableView 而不是 CellView 的位置。
您知道将 TableView Location ("World") 转换为对应的 CellView Location ("Local") 的便捷方法吗?
非常感谢。
到目前为止我的代码。
viewDidLoad:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(MyTableViewController.tapEdit(recognizer:)))
self.tableView.addGestureRecognizer(tapGesture)
tapGesture.delegate = self
点击方法:
@objc func tapEdit(recognizer: UITapGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.ended {
let tapLocation = recognizer.location(in: self.tableView)
if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
print("Tap Location: \(tapLocation)")
if let cell = self.tableView.cellForRow(at: tapIndexPath) as? MyTableViewCell {
// of course not working because of the relative location difference
if cell.myLabel.frame.contains(tapLocation) {
print("Taped cell at \(tapIndexPath). Hit Label.")
}
}
}
}
}
此致
如果您想获得该单元格的局部坐标,最简单的方法是使用它来获取该点击的位置:
@objc func tapEdit(recognizer: UITapGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.ended {
let tapLocation = recognizer.location(in: self.tableView)
if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
print("Tap Location: \(tapLocation)")
if let cell = self.tableView.cellForRow(at: tapIndexPath) as? MyTableViewCell {
let localLocation = recogniser.location(in: cell)
// of course not working because of the relative location difference
if cell.myLabel.frame.contains(localLocation) {
print("Taped cell at \(tapIndexPath). Hit Label.")
}
}
}
}
}
我的 TableView 上有一个 TapGestureRecognizer。 它让我找到了我点击过的手机。 接下来我想使用点击的位置来检查单元格上的标签是否被点击。 但当然我有相对于 TableView 而不是 CellView 的位置。 您知道将 TableView Location ("World") 转换为对应的 CellView Location ("Local") 的便捷方法吗?
非常感谢。
到目前为止我的代码。 viewDidLoad:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(MyTableViewController.tapEdit(recognizer:)))
self.tableView.addGestureRecognizer(tapGesture)
tapGesture.delegate = self
点击方法:
@objc func tapEdit(recognizer: UITapGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.ended {
let tapLocation = recognizer.location(in: self.tableView)
if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
print("Tap Location: \(tapLocation)")
if let cell = self.tableView.cellForRow(at: tapIndexPath) as? MyTableViewCell {
// of course not working because of the relative location difference
if cell.myLabel.frame.contains(tapLocation) {
print("Taped cell at \(tapIndexPath). Hit Label.")
}
}
}
}
}
此致
如果您想获得该单元格的局部坐标,最简单的方法是使用它来获取该点击的位置:
@objc func tapEdit(recognizer: UITapGestureRecognizer) {
if recognizer.state == UIGestureRecognizerState.ended {
let tapLocation = recognizer.location(in: self.tableView)
if let tapIndexPath = self.tableView.indexPathForRow(at: tapLocation) {
print("Tap Location: \(tapLocation)")
if let cell = self.tableView.cellForRow(at: tapIndexPath) as? MyTableViewCell {
let localLocation = recogniser.location(in: cell)
// of course not working because of the relative location difference
if cell.myLabel.frame.contains(localLocation) {
print("Taped cell at \(tapIndexPath). Hit Label.")
}
}
}
}
}