根据超级视图在ios中的中心位置获取图像的x和y坐标



我试图找到一个可以相对于视图中心位置缩放和移动的图像的x和y坐标。在任何设备中,一个位置的x和y坐标都应该相同。这就是为什么我想找到图像位置坐标,因为无论设备如何,图像分辨率都不会改变。我已经附上了我迄今为止尝试做的源代码。有人能给我一个建议吗
GitHub项目的链接

我可以通过触摸图像来获得准确的图像坐标,这就是我尝试的方式,但我想使用按键点击事件来获得坐标

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

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

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

最新更新