gpt4 book ai didi

objective-c - 连续显示 NSTimeInterval

转载 作者:行者123 更新时间:2023-11-29 05:03:17 33 4
gpt4 key购买 nike

刚接触 iOS SDK 和 Objective-C,有一个关于 NSTimeInterval 的简单问题。

我有一个非常简单的游戏,我想显示游戏已经玩了多长时间(以便我可以节省最佳时间)。

现在,当然,如果我使用如下内容设置标签的文本:

NSTimeInterval elapsedTime = [startDate timeIntervalSinceNow];
theLabel.text = [NSString stringWithFormat:@"%.2f", -elapsedTime];

标签的文本将是自游戏开始以来已经过去的时间,即 0 秒。

如何让 elapsedTime 对象“在后台运行”,以便玩家可以随时了解他/她玩了多长时间?

最佳答案

看看 NSTimer 类如何在预设时间安排计时器并更新回调中的标签。

-(void)viewDidLoad {
[super viewDidLoad];

// Add timer at 60 frames a second
timer = [NSTimer scheduledTimerWithInterval:1.0/60.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats: YES];
}

-(void) targetMethod: NSTimer * theTimer {
... update label
}

关于objective-c - 连续显示 NSTimeInterval,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6328797/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com