UIScreen.main.nativeBounds如何与x和y坐标相关?

时间:2019-12-27 15:51:38

标签: swift sprite-kit coordinates uiscreen

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

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

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

会将我带到设备x轴的远端,就像这样:

enter image description here

表示总计x-axis总计必须为750。 1024发生了什么事?我认为我必须误解x-coordinatesUIScreen.main.nativeBounds.height的关系。我想了解这一点的原因是因为我想动态设置x position以便它可以在所有设备上使用。类似于x: -(UIScreen.main.nativeBounds.height / 3)。我想念什么?

2 个答案:

答案 0 :(得分:0)

UIScreen.bounds指定“以点为单位的屏幕边界矩形”(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)

答案 1 :(得分:0)

您可以通过以下方式获得屏幕的高度和宽度:

let height = UIScreen.main.bounds.height
let width = UIScreen.main.bounds.width

屏幕上没有任何点具有负坐标:

屏幕的左上角是x: 0, y :0

屏幕的右上角是x: width, y: 0

屏幕的左下角是x: 0, y: height

屏幕的右下角是x: width, y: height