我试图根据用户使用的设备使场景变得不同,并且除了应用程序加载时,此方法一直很有效。例如,应用程序加载并且屏幕大小不正确,但是当我转到新场景时,大小将达到它的预期。即使你回到起始场景,它也会是正确的尺寸。它仅在应用程序首次加载时关闭,直到您转到新场景。这是代码,任何帮助将不胜感激。
- (void)viewDidLoad
{
[super viewDidLoad];
iphone4x = 1.2;
iphone4y = 1.4;
iphone5x = 1.1;
iphone5y = 1.18;
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = NO;
skView.showsNodeCount = NO;
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = YES;
CGSize newSize;
if(iPhone4) {
newSize = CGSizeMake(skView.bounds.size.width * iphone4x, skView.bounds.size.height * iphone4y);
}
if (iPhone5) {
newSize = CGSizeMake(skView.bounds.size.width * iphone5x, skView.bounds.size.height * iphone5y);
}
if (iPhone6) {
newSize = CGSizeMake(skView.bounds.size.width, skView.bounds.size.height);
}
if(iPhone6Plus) {
}
// Create and configure the scene.
SKScene *scene = [MainMenu sceneWithSize:newSize];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
答案 0 :(得分:1)
默认情况下,视图以纵向方式加载,因此即使应用程序应以横向方式运行,也可能会使用纵向模式的坐标。这是一种已知的行为。因此,如果您在横向模式下运行应用程序,那么这可能是个问题。
在调用viewWillLayoutSubviews方法时,视图的大小将是正确的。
尝试使用viewWillLayoutSubviews而不是viewDidLoad:
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
skView.showsDrawCount = YES;
//skView.showsQuadCount = YES;
skView.showsPhysics = YES;
skView.ignoresSiblingOrder = YES;
//you have to check if scene is initialised because viewWillLayoutSubviews can be called more than once
if(!skView.scene){
// Create and configure the scene.
//instead of this from your code
//SKScene *scene = [MainMenu sceneWithSize:newSize];
//use this line
MainMenu * scene = [MainMenu sceneWithSize:newSize];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
}