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

我试过了

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

他们都没有工作。

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

您可以使用下面的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.") } 

如果您想获得播放器可以看到的正确视图,则可以使用以下指南 ,如设备大小的边界框中所示。