Search code examples
swifttouchesbegan

Get various coordinates for touches in swift


Im trying to get the coordinates of various points when touching in a UIView for swift 4. I've seen the other post regarding a similar problem, but that code only allows the first touch to be registered. I would appreciate some help. Thanks.


Solution

  • This code will give you coordinates for every time you touch the screen. You can test it by print it out or put it straight in to a label.

    @IBOutlet weak var imageView: UIImageView!
    var coordinates = CGPoint.zero
    
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            if let touch = touches.first{
            coordinates = touch.location(in: imageView)
            print(coordinates)
            textLabel.text = "\(coordinates)"
        }
    }