【发布时间】:2023-03-18 22:38:01
【问题描述】:
我正在尝试在 SpriteKit 中制作简单的进度条。为了简化示例,我将使用 SKLabelNode 和它的 text 属性,它将指示进度。
这里是代码(GameScene.m):
#import "GameScene.h"
@interface GameScene ()
typedef void (^CompletitionHandler)(void);
@property (nonatomic,strong)SKLabelNode* progressBar;
@end
@implementation GameScene
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
self.progressBar = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
self.progressBar.fontColor = [SKColor redColor];
self.progressBar.fontSize = 24.0;
self.progressBar.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
[self addChild:self.progressBar];
[self loadSceneAssetsWithCompletionHandler:^{
[self setupScene:view];
}];
}
- (void)loadSceneAssetsWithCompletionHandler:(CompletitionHandler)handler {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// Load the shared assets in the background.
//This part simulates bunch of assets(textures, emitters etc) loading in background
for (int i = 0; i <= 100; i++) {
[NSThread sleepForTimeInterval:0.01];
float progressValue = (float)i;
dispatch_async(dispatch_get_main_queue(), ^{
self.progressBar.text = [NSString stringWithFormat:@"%1.f", progressValue];
});
}
if (!handler) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
// Call the completion handler back on the main queue.
handler();
});
});
}
现在我试图找到一种明显的方法,如果有的话,根据从后台队列加载的数据的百分比来更新进度。
问题是我想显示从 0 到 100 的加载数据的百分比。但我不知道如何判断 1% 的纹理、发射器和节点,或者我可以说,我不知道'不知道如何在加载 %1 后更新进度条。这是因为我正在使用不同类型的对象。有没有办法检查某些后台队列的状态,看看还有多少东西要执行(加载)?
有人有任何想法或建议吗?
【问题讨论】:
标签: ios objective-c sprite-kit progress-bar grand-central-dispatch