Search code examples
iosswiftuitouchcgpoint

Scale touch points for different screen sizes


Given a CGPoint touch point, is there a way to scale the x, y values in a way that they are going to be consistent for all screen sizes/resolutions (different iPads, different iPhones)?


Solution

  • Settle on a standard size and multiply the point you want to interpolate with the quotient of the current width/height and the standard width/height.

    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)