缩放不同屏幕尺寸的触摸点

时间:2018-02-12 14:52:27

标签: ios swift uitouch cgpoint

给定一个CGPoint触摸点,有没有办法按照它们对所有屏幕尺寸/分辨率(不同的iPad,不同的iPhone)保持一致的方式缩放x,y值?

1 个答案:

答案 0 :(得分:1)

设置标准尺寸,并将要插值的点乘以当前宽度/高度与标准宽度/高度的商。

guard let currentSize = (UIApplication.shared.delegate as? AppDelegate)?.window?.bounds.size else {
    return
}

let standardSize = CGSize(width: 320, height: 568)
let point = CGPoint(x: 120, y: 120)
let interpolatedPoint = CGPoint(x: point.x * currentSize.width / standardSize.width, y: point.y * currentSize.height / standardSize.height)