首页 > 解决方案 > UIScreen.main.nativeBounds 与 x 和 y 坐标有何关系?

问题描述

如果我print UIScreen.main.nativeBounds.height返回2048我的 iPad 模拟器,那很好。但这与x and y coordinates? 例如,屏幕中间是x: 0, y: 0。如果我向左移动,x coordinates我使用减号,比如x: -100,如果我向右移动,我使用加号,比如x: 100

在我看来,这意味着它UIScreen.main.nativeBounds.height除以 2,以便我可以在x: -1024和之间移动x: 1024。那是对的吗?好吧,似乎不是因为这段代码:

portrait = gui.addPortrait(width: 80, height: 80, x:-350 , y:  180)
addChild(portrait)

将带我到设备 x 轴的远端,如下所示:

在此处输入图像描述

这意味着总数x-axis必须是总共 750 左右。1024怎么了?我想我一定误解了它x-coordinatesUIScreen.main.nativeBounds.height. 我想了解这一点的原因是因为我想x position动态设置它以使其适用于所有设备。类似的东西x: -(UIScreen.main.nativeBounds.height / 3)。我错过了什么?

标签: swiftsprite-kitcoordinatesuiscreen

解决方案


相比之下UIScreen.bounds,指定“[t] 屏幕的边界矩形,以点为单位”(docs),UIScreen.nativeBounds指定“[t] 物理屏幕的边界矩形,以像素为单位”(docs)。

因此,您需要借助以下方法将值转换为点UIScreen.nativeScale

let width = round(UIScreen.main.nativeBounds.width / UIScreen.main.nativeScale)
let height = round(UIScreen.main.nativeBounds.height / UIScreen.main.nativeScale)

推荐阅读