IOS X,Y坐标从图像

时间:2021-12-23 21:22:04

I am new to IOS Programming coming from Android I am Trying to get The X,Y Coordinates for a double tap on the screen but I need the coords in relation to the image itself and not the image view.

我是来自Android的IOS编程的新手我试图获得X,Y坐标在屏幕上双击,但我需要与图像本身相关的坐标而不是图像视图。

This is my view controller code

这是我的视图控制器代码

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)
}


}

This works ok and when double tapped it prints the coordinates

这可以正常工作,当双击它打印坐标

The Image is a PNG inside an Imageview which in turn is inside a Scrollview

Image是Imageview中的PNG,后者又位于Scrollview内

The PNG has the dimensions 6400px X 4100px but if I double tap at the bottom right of the image view instead of getting 6400,4100 I get 478,415 so it is obviously using the coords from the image view

PNG的尺寸为6400px X 4100px但是如果我在图像视图的右下角双击而不是得到6400,4100我得到478,415所以它显然是使用图像视图中的坐标

In android I would use PointF to get these

在android中我会使用PointF来获取这些

Is there an equivalent in Swift

在Swift中是否有相同的东西

I am using Xcode 7 and Swift 2.0

我正在使用Xcode 7和Swift 2.0

Any Help is appreciated

任何帮助表示赞赏

Mark

1 个解决方案

#1


0  

You need to capture following two things :

您需要捕获以下两件事:

  1. Image size
  2. Frame size (i.e. imageView size)
  3. 帧大小(即imageView大小)

With this, you need to calculate the actual touched point.

有了这个,你需要计算实际的触摸点。

Following code might help you :

以下代码可能会帮助您:

 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)")

Hope this helps.

希望这可以帮助。

#1


0  

You need to capture following two things :

您需要捕获以下两件事:

  1. Image size
  2. Frame size (i.e. imageView size)
  3. 帧大小(即imageView大小)

With this, you need to calculate the actual touched point.

有了这个,你需要计算实际的触摸点。

Following code might help you :

以下代码可能会帮助您:

 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)")

Hope this helps.

希望这可以帮助。