是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
}