简体   繁体   中英

Using UIKit's UIScrollView with SpriteKit

I read two questions here about that, both with the same answer. The suggested solution was to create the UIScrollView object, and then add it to the parent view of the SKScene in the target scene's didMoveToView:, removing it in willMoveFromView:.

And that is the target scene code I implemented:

@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

Please note that the user should go to this scene from the home scene when clicking on a button. The problem now is that once I add the line:

[self.view addSubview:_scrollView];

to add the UIScrollView object to this scene, the application no more goes to this scene and instead add the UIScrollView to the home scene I am transitioning from. It also doesn't remove it when transitioning to another scene. Once I comment out this line, everything works just as expected, clearly without presenting the UIScrollView.

I am stuck. Any help or advice is highly appreciated. Thanks!

I finally figured out what the problem was for me. I was using the -(void)willLayoutSubviews method of the viewController to ensure I had the correct bounds, but I had forgotten to see if my SKScene was already presented. That resulted in a new welcomeScene being presented each time the view was laid out on top of the old one.

-(void)willLayoutSubviews
{
    if(!self.didPresentScene)
    {
        // Present scene here
        ....
        self.didPresentScene = YES;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM