How to get contact coordinates when choosing a custom UITableViewCell?

When I touch (Touch Up), UITableViewCell my ViewController method UITableViewDelegate - (void) tableView: (UITableView *) is called tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath. I also need to get the x Touch-Point coordinate at this time, so that I can find out which part of the Cell has been affected (items not related to accessories). I tried using normal touch methods like TouchesDidEnd, but the coordinates always return x = 0.000 y = 0.000 no matter where I touched (location in a custom UITableViewCell in a UITableView object in my ViewController). I also tried to implement touch processing from the Custom Cell class, and although I get the exact coordinates, I could not find a way to pass these coordinates to my ViewController class (which has a UITableView).

Q: Is there a good way to get the x-coordinates of a device screen when I touch a custom UITableViewCell?

+6
source share
4 answers

Put transparent UIViewon top of the table view, override its methods -touchesBegan:withEvent:, etc. In these overridden methods, get the objects UITouchand call -locationInView:them to get the values CGPoint.

This gives you the coordinates of the touch event.

Then you just pass those events UITouchto the main view of the table. In the table view, the usual thing is to pass strokes to rows, etc.

+5
source

What about:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let rect = tableView.rectForRow(at: indexPath as IndexPath)
        var point = CGPoint(x: rect.midX, y: rect.midY)
        point = tableView.convert(point, to: nil)
        print(point)
}
+2
source

1. Add tapGestureRecognizer to your table view

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tableViewTapped(recognizer:)))
tableView.addGestureRecognizer(tapGestureRecognizer)

2. Write a function to handle 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!")        
            }
        }
    }
}
0
source

1) Transfer the view of the object controller to the user cell

class ImageTableViewCell: UITableViewCell {

      @IBOutlet weak var img: UIImageView!

      var vc: UIViewController? = nil

      var delegate: TouchLocation?

}

2) Override the user table view cell with the touchesBegan method and override the super touchesBegan method

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    delegate?.touchLocation(location: (touch?.location(in: self.vc?.view))!)
    if let aTouches = touches as? Set<UITouch>, let anEvent = event {
        super.touchesBegan(aTouches, with: anEvent)
    }
}

3) Get the location and go to viewing the controller using delegation

protocol TouchLocation {
   func touchLocation(location: CGPoint)
}

extension ImageListViewController: TouchLocation{

   func touchLocation(location: CGPoint) {
         self.touchedPosition = location // Here you will get touch location 
   }

}
0
source

Source: https://habr.com/ru/post/1715161/


All Articles