简体   繁体   English

获取 UIView 原点与屏幕尺寸 iOS

[英]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.我想从一台设备获取 UIView 原点并将其保存到服务器。 On other device, I want to show UIView at same origin points.在其他设备上,我想在相同的原点显示 UIView。 The problem is coming with screen size.问题在于屏幕尺寸。

Suppose I get UIView position in iPhone 11 and save it on server.假设我在 iPhone 11 中获得 UIView position 并将其保存在服务器上。 Now, I get saved position from server on iPhone 6s device and show UIView on that position.现在,我从 iPhone 6s 设备上的服务器上保存了 position,并在该 position 上显示 UIView。 UIView location is not same on both devices because of screen sizes are different.由于屏幕尺寸不同,UIView 在两种设备上的位置都不相同。

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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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