Reputation: 1375
I have the following UI hierarchy:
UIView -> UIScrollView -> UIImageView
It was necessary because i wanted to zoom my image in and out. So far so good and everything works. Now i wanted to detect the touch location of the image. I've read already a lot and it works if i just have an UIImage in an UIView. Unfortunately i need it for the first described hierarchy.
class TestController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var myImageView: UIImageView!
@IBOutlet var myGestureRecognizer: UITapGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
self.myImageView.addGestureRecognizer(myGestureRecognizer)
myImageView.userInteractionEnabled = true
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject()! as UITouch
let location = touch.locationInView(self.myImageView)
println(location)
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return self.myImageView
}
.....
}
I've tried to add "myGestureRecognizer" to UIView and also to the UIImageView and even to all 3 (UIView, UIScrollView, UIImageView) together. It just doesn't work for me and I don't know what to do.
How can I detect the location of touches (just single tap) in a UIImageView, which is placed in a UIScrollView?
Thanks in advance!
Upvotes: 3
Views: 3507
Reputation: 39081
func didTapImage(gesture: UIGestureRecognizer) {
let point = gesture.locationInView(gesture.view)
}
override func viewDidLoad() {
super.viewDidLoad()
let imgView = UIImageView()
imgView.userInteractionEnabled = true // IMPORTANT
imgView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "didTapImage:"))
}
On UIImageView userInteractionEnabled is default to false and will not register touches.
Upvotes: 9