我在这里读到两个问题,两个问题都有相同的答案。建议的解决方案是创建UIScrollView对象,然后将其添加到目标场景的didMoveToView中的SKScene的父视图中,在willMoveFromView中删除它:。
这就是我实施的目标场景代码:
@interface MQSSMakerScene ()
@property BOOL contentCreated;
@property MQSSMakerWorkspaceLayer *workspaceLayer;
@property MQSSMakerDockLayer *dockLayer;
@property UIScrollView *scrollView;
@end
@implementation MQSSMakerScene
- (id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
CGSize layerSize = CGSizeMake(944, 140);
CGPoint layerPosition = CGPointMake(40, 768-(140+30));
CGRect viewFrame = CGRectMake(layerPosition.x, layerPosition.y, layerSize.width, layerSize.height);
_scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
_scrollView.contentSize = CGSizeMake(2000, 120);
_scrollView.scrollEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = YES;
_scrollView.backgroundColor = [UIColor brownColor];
}
return self;
}
- (void)didMoveToView:(SKView *)view
{
[super didMoveToView:view];
if(!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
[self.view addSubview:_scrollView]; // --> WHERE IT'S BEING ADDED
}
- (void)willMoveFromView:(SKView *)view
{
[super willMoveFromView:view];
[_scrollView removeFromSuperview]; // --> WHERE IT'S BEING REMOVED
}
- (void)createSceneContents
{
self.backgroundColor = [UIColor colorWithHue:.237 saturation:.56 brightness:.46 alpha:1];
self.scaleMode = SKSceneScaleModeAspectFill;
[self addChild:[self newMakerLayout]];
}
- (SKNode *)newMakerLayout
{
SKNode *mainLayout = [[SKNode alloc] init];
self.workspaceLayer = [[MQSSMakerWorkspaceLayer alloc] init];
self.dockLayer = [[MQSSMakerDockLayer alloc] init];
[mainLayout addChild:self.workspaceLayer];
[mainLayout addChild:self.dockLayer];
MQSSStoryObject *ob1 = [[MQSSStoryObject alloc] initWithImageNamed:@"maker-1.png"];
[self.workspaceLayer addChild:ob1];
return mainLayout;
}
@end
请注意,用户应在点击按钮时从家庭场景转到此场景。现在的问题是,一旦我添加了这一行:
[self.view addSubview:_scrollView];
将UIScrollView对象添加到此场景,应用程序不再进入此场景,而是将UIScrollView添加到我正在转换的主场景中。转换到另一个场景时,它也不会删除它。一旦我注释掉这一行,一切都按预期工作,显然没有呈现UIScrollView。
我被困住了。任何帮助或建议都非常感谢。谢谢!
答案 0 :(得分:2)
我终于弄清楚问题对我来说是什么。我正在使用viewController的 - (void)willLayoutSubviews方法来确保我有正确的边界,但我忘了看我的SKScene是否已经呈现。这导致每次将视图放在旧视图之上时都会出现一个新的welcomeScene。
-(void)willLayoutSubviews
{
if(!self.didPresentScene)
{
// Present scene here
....
self.didPresentScene = YES;
}
}