0

On touching on the image I want to get coordinate of that pixel, I tried some code but not getting exact location where the user touches on the image.

This my existing code

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let position = touch.location(in: self)


        let Z1 = self.image!.size.height

        let Z2 = self.image!.size.width

        let Z3 = self.bounds.minY

        let Z4 = self.bounds.minX

        let Z5 = self.bounds.height

        let Z6 = self.bounds.width

        let pos1 = (position.x - Z4) * Z2 / Z6

        let pos2 = (position.y - Z3) * Z1 / Z5

        let point = CGPoint(x: pos1, y: pos2)
    }
}
  • Can you post the code that you have tried? – Sweeper Aug 22 '19 at 06:53
  • let Z1 = ImageView.image!.size.height let Z2 = ImageView.image!.size.width let Z3 = ImageView.bounds.minY let Z4 = ImageView.bounds.minX let Z5 = ImageView.bounds.height let Z6 = ImageView.bounds.width let pos1 = (position.x - Z4) * Z2 / Z6 let pos2 = (position.y - Z3) * Z1 / Z5 let point = CGPoint(x: pos1, y: pos2) i have tried with this code – Fayyaz Hussain Aug 22 '19 at 06:55
  • You should [edit] your question with that code. Also, it doesn't seem like you are getting any user input from anywhere. Are you doing this in a `touchesBegan` method or something like that? – Sweeper Aug 22 '19 at 06:57

1 Answers1

2

Is your touchesBegan getting called ? If so this should be enough..

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let pos = touch.location(in: imageView)
            print(pos.x)
            print(pos.y)
        }
    }
mentoxska
  • 135
  • 9