【发布时间】:2016-07-12 14:34:16
【问题描述】:
我有一个大小为480x360 的图像,并在UIImageview 中显示,框架为(0, 0, 320, 568) 和aspectfill。
当我触摸图像视图时,位置显示(150,120)。如何以我的原始图像(480x360) 格式映射此位置?
谢谢
【问题讨论】:
标签: ios uiimageview uitouch
我有一个大小为480x360 的图像,并在UIImageview 中显示,框架为(0, 0, 320, 568) 和aspectfill。
当我触摸图像视图时,位置显示(150,120)。如何以我的原始图像(480x360) 格式映射此位置?
谢谢
【问题讨论】:
标签: ios uiimageview uitouch
您只需计算比例并将它们应用于触摸位置点。
CGFloat _x = imageView.image.size.width / imageView.frame.size.width;
CGFloat _y = imageView.image.size.height / imageView.frame.size.height;
//assuming p - is CGPoint received from Touch event
CGPoint locationInImageView = CGPointMake(p.x * _x, p.y * _y);
【讨论】:
在 Swift 中查找触摸的像素位置:
guard let image = imageView.image else {
return
}
let x = image.size.width / imageView.frame.size.width
let y = image.size.height / imageView..frame.size.height
// Assuming touchLocation is CGPoint received from Touch event
var touchLocation: CGPoint
let locationInImageView = CGPoint(x: touchLocation.x * x, y: touchLocation.y * y);
【讨论】: