我一直在尝试自学 Objective-C,但我在 View 方面遇到了一些问题。
将所有内容从 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
以避免每次 View 旋转时重新创建手势识别器。谢谢你的帮助!
最佳答案
你为什么这么做?初始化 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/