在 Swift / IOS 中获取实际视图大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36344580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting actual view size in Swift / IOS
提问by Charles
Even after reading several posts in frame vs view I still cannot get this working. I open Xcode (7.3), create a new game project for IOS. On the default scene file, right after addChild, I add the following code:
即使在 frame vs view 中阅读了几篇文章后,我仍然无法正常工作。我打开 Xcode (7.3),为 IOS 创建一个新的游戏项目。在默认场景文件中,在 addChild 之后,我添加以下代码:
print(self.view!.bounds.width)
print(self.view!.bounds.height)
print(self.frame.size.width)
print(self.frame.size.height)
print(UIScreen.mainScreen().bounds.size.width)
print(UIScreen.mainScreen().bounds.size.height)
I get following results when I run it for iPhone 6s:
当我为 iPhone 6s 运行它时,我得到以下结果:
667.0
375.0
1024.0
768.0
667.0
375.0
I can guess that first two numbers are Retina Pixel size at x2. I am trying to understand why frame size reports 1024x768 ?
我可以猜到前两个数字是 x2 的视网膜像素大小。我想了解为什么帧大小报告 1024x768 ?
Then I add following code to resize a simple background image to fill the screen:
然后我添加以下代码来调整一个简单的背景图像以填充屏幕:
self.anchorPoint = CGPointMake(0.5,0.5)
let theTexture = SKTexture(imageNamed: "intro_screen_phone")
let theSizeFromBounds = CGSizeMake(self.view!.bounds.width, self.view!.bounds.height)
let theImage = SKSpriteNode(texture: theTexture, color: SKColor.clearColor(), size: theSizeFromBounds)
I get an image smaller than the screen size. Image is displayed even smaller if I choose landscape mode.
我得到的图像小于屏幕尺寸。如果我选择横向模式,图像会显示得更小。
I tried multiplying bounds width/height with two, hoping to get actual screen size but then the image gets too big. I also tried frame size which makes the image slightly bigger than the screen.
我尝试将边界宽度/高度乘以两个,希望得到实际的屏幕尺寸,但图像变得太大了。我还尝试了使图像略大于屏幕的帧大小。
Main reason for my confusion, besides lack of knowledge is the fact that I've seen this exact example on a lesson working perfectly. Either I am missing something obvious or ?
除了缺乏知识之外,我感到困惑的主要原因是我在一节课上看到了这个确切的例子,效果很好。要么我错过了一些明显的东西,要么?
回答by Wes
The frame is given as 1024x768 because it is defined in points, not pixels.
帧被指定为 1024x768,因为它是以点而不是像素定义的。
If you want your scene to be the same size as your screen, before the scene is presented in your GameViewController, before:
如果您希望您的场景与您的屏幕大小相同,那么在您的 GameViewController 中呈现场景之前,请执行以下操作:
skView.presentScene(scene)
skView.presentScene(scene)
use this:
用这个:
scene.size = self.view.frame.size
scene.size = self.view.frame.size
which will make the scene the exact size of the screen.
这将使场景精确到屏幕的大小。
Then you could easily make an image fill the scene like so:
然后,您可以轻松地使图像填充场景,如下所示:
func addBackground() {
let bgTexture = SKTexture(imageNamed: "NAME")
let bgSprite = SKSpriteNode(texture: bgTexture, color: SKColor.clearColor(), size: scene.size)
bgSprite.anchorPoint = CGPoint(x: 0, y: 0)
bgSprite.position = self.frame.origin
self.addChild(bgSprite)
}
Also, you may want to read up on the difference between a view's bounds and it's frame.
此外,您可能想了解视图边界和框架之间的区别。