我有以下UI层次结构:
UIView - > UIScrollView - >的UIImageView
这是必要的,因为我想放大和缩小我的图像。到目前为止一切都很好,一切正常。现在我想检测图像的触摸位置。我已经阅读了很多,如果我只是在UIView中有一个UIImage,它就有效。不幸的是,我需要它用于第一个描述的层次结构。
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
}
.....
}
我试图将“myGestureRecognizer”添加到UIView以及UIImageView甚至所有3(UIView,UIScrollView,UIImageView)。它对我不起作用,我不知道该怎么做。
如何在放置在UIScrollView中的UIImageView中检测触摸位置(只需单击)?
提前致谢!
答案 0 :(得分:9)
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:"))
}
在UIImageView上,userInteractionEnabled默认为false,不会注册触摸。