我是来自Android的IOS编程新手我试图获得X,Y坐标在屏幕上双击,但我需要与图像本身相关的坐标,而不是图像视图。
这是我的视图控制器代码
class ThirdViewController: UIViewController, UIScrollViewDelegate
{
@IBOutlet var scrollView: UIScrollView!
@IBOutlet weak var imageView: UIImageView!
var firstPass:String!
var secondPass:String!
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.minimumZoomScale=1.0
self.scrollView.maximumZoomScale=20.0
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("tapAction:"))
tapGestureRecognizer.numberOfTapsRequired=2
self.imageView.userInteractionEnabled = true
self.imageView.addGestureRecognizer(tapGestureRecognizer)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return self.imageView
}
func tapAction(sender: UITapGestureRecognizer) {
let touchPoint = sender.locationInView(self.imageView)
let Z1:String = String(touchPoint.x)
let Z2:String = String(touchPoint.y)
print( "YES It Works X= " + Z1 + " Y= " + Z2)
}
print( "It Works X= " + Z1 + " Y= " + Z2)
}
}
这个工作正常,双击时会打印坐标
图像是Imageview中的PNG,而后者又位于Scrollview内部
PNG的尺寸为6400px X 4100px但是如果我在图像视图的右下角双击而不是获得6400,4100我得到478,415,所以它显然是使用图像视图中的坐标
在android中我会使用PointF来获取这些
Swift中是否有等效的
我正在使用Xcode 7和Swift 2.0
感谢任何帮助
标记
答案 0 :(得分:1)
您需要捕获以下两件事:
有了这个,你需要计算实际的触摸点。
以下代码可能会对您有所帮助:
var touchPoint = gestureRecognizer.locationInView(self.photoImageView)
touchPoint.x = touchPoint.x * (photoImageView.image?.size.width)! / photoImageView.frame.width
touchPoint.y = touchPoint.y * (photoImageView.image?.size.height)! / photoImageView.frame.height
print("Touched point (\(touchPoint.x), \(touchPoint.y)")
希望这有帮助。