简体   繁体   中英

get UIView origin with aspect to screen size iOS

I want to get UIView origin points from one device and save that points to server. On other device, I want to show UIView at same origin points. The problem is coming with screen size.

Suppose I get UIView position in iPhone 11 and save it on server. Now, I get saved position from server on iPhone 6s device and show UIView on that position. UIView location is not same on both devices because of screen sizes are different.

Is there any solution for it? Is there any way to get origin points aspect with screen size that will be fine for all screen sizes?

1st device

let yourPositionOnView: CGPoint = ...
let screenPosition = yourView.convert(yourPositionOnView, to: UIScreen.main)
let relativePosition = CGPoint(
    x: screenPosition.x / UIScreen.main.bounds.width, 
    y: screenPosition.y / UIScreen.main.bounds.height
)

2nd device

let screenPosition = CGPoint(
    x: relativePosition.x * UIScreen.main.bounds.width, 
    y: relativePosition.y * UIScreen.main.bounds.height
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM