How to get touch coordinates upon SELECTING a custom UITableViewCell?
Put a transparent UIView
atop your table view, override its -touchesBegan:withEvent:
etc. methods. In those overridden methods, get the UITouch
objects and call -locationInView:
on them to get the CGPoint
values.
That gives you the coordinates of a touch event.
Then you just pass on those UITouch
events to the underlying table view. The table view handles its usual business of passing touches on to rows, etc.
1. Add a tapGestureRecognizer to your tableView
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tableViewTapped(recognizer:)))
tableView.addGestureRecognizer(tapGestureRecognizer)
2. Write a function to handle the tapGesture
@objc func tableViewTapped(recognizer: UITapGestureRecognizer) {
let location = recognizer.location(in: self.tableView) // point of touch in tableView
if let indexPath = self.tableView.indexPathForRow(at: location) { // indexPath of touch location
if let cell = tableView.cellForRow(at: indexPath) as? MyCustomCell {
let locationInCell = recognizer.location(in: cell) // point of touch in cell
// do something with location or locationInCell
if cell.imageView.frame.contains(location) || cell.imageView.frame.contains(locationInCell) {
print("ImageView inside cell tapped!")
}
}
}
}