【问题标题】:Get x and y coordinates of an image according to the superview's centre position in ios根据superview在ios中的中心位置获取图片的x和y坐标
【发布时间】:2018-08-02 05:23:27
【问题描述】:

我正在尝试查找可以相对于视图中心位置缩放和移动的图像的 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)")
}

【问题讨论】:

  • 因此,您需要相对于视图的图像坐标。对吗?
  • 是的,我想在不点击图像中的位置的情况下获取相对于视图的图像坐标。

标签: ios swift


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多