我一直在试图自学Objective-C,但是我在观点方面遇到了麻烦。
将所有内容从viewWillLayoutSubviews
移到viewDidLoad
后,我的场景不再显示。 MyScene.m中的代码正在运行,我只是看不到它。我在这里缺少一些明显的缺陷吗?
前面的viewWillLayoutSubviews
方法在下面可用。此处的所有内容都直接切成viewDidLoad
,不包括[super viewDidLayoutSubviews]
。
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
SKView * skView = (SKView *)self.view;
if (!skView.scene)
{
skView.showsFPS = YES;
skView.showsNodeCount = YES;
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:scene];
}
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:(scene) action:@selector(screenSwipedRight)];
swipeRight.numberOfTouchesRequired = 1;
swipeRight.direction=UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:(swipeRight)];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:(scene) action:@selector(screenSwipedLeft)];
swipeLeft.numberOfTouchesRequired = 1;
swipeLeft.direction=UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:(swipeLeft)];
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:(scene) action:@selector(screenSwipedUp)];
swipeUp.numberOfTouchesRequired = 1;
swipeUp.direction=UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:(swipeUp)];
}
如果有帮助,我将所有内容从
viewWillLayoutSubviews
移到viewDidLoad
,以避免每次视图旋转时重新创建手势识别器。谢谢你的帮助!
最佳答案
你为什么这么做?可接受的初始化SKScene的方法在viewWillLayoutSubviews
中。
只需在!skView.scene
块内创建手势识别器即可:
if (!skView.scene)
{
// init everything here that should be initialized only once
// AND should not re-run every time the view rotates
}
就这么简单。
关于ios - 从viewWillLayoutSubviews移到viewDidLoad后, View 未显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20925897/