4
votes

I'm working on a UIImageView inside a UIScrollView, the UIImageView is made zoomable through the UIScrollView but my goal is to get the coordinates of a point, so that whenever the user touches the UIImageView while zoomed the point "sticks" to the UIImageView, then when unzoomed (so when the zoom scale is at 1.0), the coordinates would be saved.

I was able to get the picture, put it in an ImageView in a ScrollView and I made the picture zoomable, but whenever I touch the scrollView no coordinates are being showed.

This is my code:

    override func viewDidLoad() {
    super.viewDidLoad()

    self.scrollview.minimumZoomScale = 0.5
    self.scrollview.maximumZoomScale = 6.0
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

@IBAction func ChooseExisting(sender: AnyObject) {

    let picker = UIImagePickerController()

    picker.delegate = self
    picker.sourceType = .PhotoLibrary

    presentViewController(picker, animated: true, completion: nil)


}

@IBAction func Camera(sender: AnyObject) {
    let picker = UIImagePickerController()

    picker.delegate = self
    picker.sourceType = .Camera

    presentViewController(picker, animated: true, completion: nil)



}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    ChosenImage.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    dismissViewControllerAnimated(true, completion: nil)
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
    return self.ChosenImage
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: (UIEvent!)) {
    if let touch = touches.first as? UITouch {
        let location = touch.locationInView(self.ChosenImage)

        println("\(location)")
    }
}

println only returns values for touches anywhere but in the ScrollView

2
Make sure to set UIImageview's userInteractionEnabled = trueuser523234
You should using gesture. Add gesture to UIImageViewtuledev

2 Answers

4
votes

Add a UIGestureRecognizer to your UIImageView and set userInteractionEnabled to true.

let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("imageTapped:"))
imgViewClickTest.addGestureRecognizer(tapGestureRecognizer)
imgViewClickTest.userInteractionEnabled = true

And then handle the tap

func imageTapped(recognizer : UIGestureRecognizer) {
    let tappedPoint: CGPoint = recognizer.locationInView(self.view!)
    let x: CGFloat = tappedPoint.x
    let y: CGFloat = tappedPoint.y

    print(tappedPoint)
    print(x)
    print(y)
}
2
votes

Adding UIGestureRecognizer to your UIImageView should be fixing your problem.