刚刚接触iOS开发工具包和Objective-C,并且只有一个关于NSTimeInterval的快速问题。
我有一个非常简单的游戏,我想显示游戏已经玩了多长时间(这样我就可以节省最好的时间)。
现在,当然,如果我将标签的文本设置为这样:
NSTimeInterval elapsedTime = [startDate timeIntervalSinceNow];
theLabel.text = [NSString stringWithFormat:@"%.2f", -elapsedTime];标签的文本将是从游戏开始到现在所经过的时间,即0秒。
我怎样才能让elapsedTime对象“在后台运行”,这样玩家才能知道他/她一直玩了多久?
发布于 2011-06-13 17:53:42
看一下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
}https://stackoverflow.com/questions/6328797
复制相似问题