I have a TapGestureRecognizer on my TableView. It get me the cell I tapped on. Next I want to use the tapped location to check if a Label on the cell was tapped. But of course I have the location relative to the TableView and not to the CellView. Do you know a convenient way to transform a TableView Location ("World") to the corresponding CellView Location ("Local")?
Thank you very much.
My code so far. viewDidLoad:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(MyTableViewController.tapEdit(recognizer:)))
self.tableView.addGestureRecognizer(tapGesture)
tapGesture.delegate = self
Tap Method:
@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.")
}
}
}
}
}
Regards
The simplest way if you want to have the local coordinate of that cell, you should use it to get the location of that tap:
@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.")
}
}
}
}
}