如果我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: -1024
和x: 1024
之间移动。那是对的吗?好吧,似乎不是因为这段代码:
portrait = gui.addPortrait(width: 80, height: 80, x:-350 , y: 180)
addChild(portrait)
会将我带到设备x轴的远端,就像这样:
表示总计x-axis
总计必须为750。 1024发生了什么事?我认为我必须误解x-coordinates
与UIScreen.main.nativeBounds.height
的关系。我想了解这一点的原因是因为我想动态设置x position
以便它可以在所有设备上使用。类似于x: -(UIScreen.main.nativeBounds.height / 3)
。我想念什么?
答案 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