Delay when unhiding UIButtons after presenting SKS

2019-02-18 04:48发布

I have encountered a delay/pause that I was not expecting and the reason so far has me scratching my head. I have a simple game setup where the UIViewController shows a number of UIButtons [PLAY GAME] [VIEW SCORES] etc. which in turn present a different SKScene

My problem is that when I try and set the visibility of these buttons to visible (perviously set to hidden in viewDidLoad) from the UIViewController they take about 5 seconds to actually show.

@implementation ViewController
- (void)presentTitleScene {

    // SHOW BUTTONS
    [[self logoLabel] setHidden:NO];
    [[self gameButton] setHidden:NO];
    [[self scoreButton] setHidden:NO];
    [[self creditsButton] setHidden:NO];

    // PRESENT SCENE
    SKScene *titleScene = [TitleScene sceneWithSize:[[self spriteKitView] bounds].size];
    [titleScene setName:@"TITLE_SCENE"];
    [titleScene setScaleMode:SKSceneScaleModeAspectFill];
    [(SKView *)[self view] presentScene:titleScene];
    [self setCurrentScene:titleScene];
}
@end

What happens is all the code runs, the SKScene presents correctly, then after about 5-6 seconds the buttons appear? Is there anything I can do about this (force an update) or is it just a case of design it out or live with it?

This happens on both the simulator and the device.

EDIT:

Looking at the output log you can clearly see that after calling preloadTextureAtlases:withCompletionHandler: that execution jumps to another thread. The method preloadTextureAtlases:withCompletionHandler: is called on the main thread and its supposed to preload the textureAtlas(s) on a background thread, but I was under the impression that the completionHandler would call back onto the main thread, is this assumption right or am I wrong?

EDIT_002:

Moved to answer below.

1条回答
叛逆
2楼-- · 2019-02-18 05:40

With regards to preloadTextureAtlases:withCompletionHandler: the completionHandler gets called on a background thread, I would assume the same one that was used to preload the atlases. The problem that I was having was that I was using the completion handler to send an NSNotification to my viewController saying "the assets have loaded, start the game" The issue with this is that "Notifications are delivered in the same thread that they are sent from" so my game also started in the background thread. As a consequence the code that set the UIButtons to visible was also running on that same background thread, hence the delay in them reacting to either being made visible or hidden.

查看更多
登录 后发表回答