在呈现 SKScene 后取消隐藏 UIButtons 时延迟? [英] Delay when unhiding UIButtons after presenting SKScene?

查看:12
本文介绍了在呈现 SKScene 后取消隐藏 UIButtons 时延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个我没有预料到的延迟/暂停,到目前为止的原因让我摸不着头脑.我有一个简单的游戏设置,其中 UIViewController 显示了许多 UIButtons [PLAY GAME] [VIEW SCORES] 等,它们依次呈现不同的 SKScene

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

我的问题是,当我尝试从 UIViewController 将这些按钮的可见性设置为可见(以前在 viewDidLoad 中设置为隐藏)时,它们大约需要 5 秒实际显示.

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

如果所有代码运行,SKScene 正确显示,然后大约 5-6 秒后出现按钮,会发生什么情况?对此我能做些什么(强制更新),还是只是设计出来或接受它?

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.

查看输出日志可以清楚地看到,调用preloadTextureAtlases:withCompletionHandler:之后,执行跳转到了另一个线程.preloadTextureAtlases:withCompletionHandler: 方法在主线程上调用,它应该在后台线程上预加载 textureAtlas(s),但我的印象是,completionHandler 会回调到主线程,这个假设是对还是我错了?

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?

移到下面回答.

推荐答案

关于 preloadTextureAtlases:withCompletionHandler:completionHandler 在后台线程上被调用,我会假设用于预加载图集的同一个.我遇到的问题是我正在使用完成处理程序向我的 viewController 发送一个 NSNotification 说资产已加载,开始游戏"这个问题是通知在发送它们的同一线程中传递from" 所以我的游戏也从后台线程开始.因此,将 UIButtons 设置为可见的代码也在同一个后台线程上运行,因此它们对变为可见或隐藏的反应会有延迟.

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.

这篇关于在呈现 SKScene 后取消隐藏 UIButtons 时延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆