我正在使用UIScrollView内部的UIImageView,UIImageView可以通过UIScrollView进行缩放,但我的目标是获取一个点的坐标,这样每当用户触摸UIImageView同时放大点“粘”到UIImageView,然后当unzoomed时(所以当缩放比例为1.0时),坐标将被保存。
我能够得到图片,将它放在ScrollView中的ImageView中,我将图片缩放,但每当我触摸scrollView时,都没有显示坐标。
这是我的代码:
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仅返回ScrollView
中任何位置的触摸值答案 0 :(得分:4)
向UIGestureRecognizer
添加UIImageView
并将userInteractionEnabled
设置为true
。
let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("imageTapped:"))
imgViewClickTest.addGestureRecognizer(tapGestureRecognizer)
imgViewClickTest.userInteractionEnabled = true
然后处理点击
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)
}
答案 1 :(得分:2)
将UIGestureRecognizer
添加到UIImageView
应该可以解决问题。