ios - ios根据superview的中心位置获取图像的x和y坐标

标签 ios swift

我正在尝试查找可以相对于 View 中心位置缩放和移动的图像的 x 和 y 坐标。一个位置的 x 和 y 坐标在任何设备中都应该相同。这就是为什么我想要查找图像位置坐标,因为无论设备如何,图像分辨率都不会改变。我已附上迄今为止我尝试做的源代码。谁能给我一个建议来做到这一点?
link to the GitHub project

我能够通过触摸图像来获取准确的图像坐标,这就是我尝试的方法,但我想通过按钮单击事件使用图钉来获取该坐标

    @objc func tapAction(tapGestureRecognizer: UITapGestureRecognizer)     
    {
    let touchPoint: CGPoint = tapGestureRecognizer.location(in: 
    self.imageView)
    let Z1 = imageView.image!.size.height

    let Z2 = imageView.image!.size.width

    let Z3 = imageView.bounds.minY

    let Z4 = imageView.bounds.minX

    let Z5 = imageView.bounds.height

    let Z6 = imageView.bounds.width

    let pos1 = (touchPoint.x - Z4) * Z2 / Z6

    let pos2 = (touchPoint.y - Z3) * Z1 / Z5

    let ZZ1 = "\(pos1)"

    let ZZ2 = "\(pos2)"
    tochpointvalues.text = "Touched point x:\(ZZ1), y:\(ZZ2)"
    print("Touched point (\(ZZ1), \(ZZ2)")
}

最佳答案

要获取 subview 相对于其父 View 的 (x, y) 坐标,可以使用 convert(_ :, to:) 方法。示例:

let childCoordinate =parentView.convert(childView.frame.origin, to:parentView)

关于ios - ios根据superview的中心位置获取图像的x和y坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51646049/

相关文章:

ios - 在页脚按钮上单击表格 View 不会重新加载表格的内容?

iphone - 以编程方式检测已安装的应用程序

ios - 在加载 ECSlidingViewController 错误之前继续获取 Set the topViewController

ios - 简单隧道构建失败 - NEAppProxyErrorDomain

ios - Swift 注释 "Tag"

iphone - 创建许多具有不同内容的 ViewController 并在它们之间移动

swift - 如何比较两个日期。日期转换为字符串

ios - 在 swift 代码中使用 fscanf() 函数

ios - 指向数组元素的 UnsafeMutablePointer

ios - 如何从 SKScene 到 UIViewController?