SpriteKit如何获得正确的屏幕尺寸

SpriteKit how to get correct screen size

我试过了

self.frame.size
self.view!.frame.size
UIScreen.mainScreen().bounds.size

None 他们工作。

如何获得设备的正确屏幕尺寸?

您可以使用以下 swift 代码来获取屏幕尺寸。

let displaySize: CGRect = UIScreen.mainScreen().bounds
let displayWidth = displaySize.width
let displayHeight = displaySize.height

您可以使用:

let deviceWidth = UIScreen.mainScreen().bounds.width
let deviceHeight = UIScreen.mainScreen().bounds.height

如果您需要获取比率以便检测他们使用的是什么设备,您可以将以下内容用于纵向模式或将其切换为横向模式。

let maxAspectRatio: CGFloat = deviceHeight / deviceWidth

然后您可以检查设备的比例以确定某些事情。

if maxAspectRatio == (4 / 3) {
    //The ratio of an iphone 4S is 4:3
    print("The user is using an iPhone 4S or iPod 4th Generation.")
} else if maxAspectRatio >= 1.7 && maxAspectRatio <= 1.8 {
    //The ratio of these devices is 16:9
    print("The user is using an iPhone 5, 5S, 5C, 6, 6S, 6 Plus, 6S Plus, or iPod 5th Generation.)
} else {
    print("The user is using an iPad, iPad Mini, iPad Air, iPad Retina, or iPad Pro.")
}

如果你想获得玩家可以看到的正确视图,如设备尺寸的边界框,你可以使用以下guide.