0

我正在使用 NSTimer,代码如下:

- (IBAction)start:(id)sender {

    MainInt = 0;

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countup) userInfo:nil repeats:YES];

}

我怎样才能显示毫秒和分钟呢?

没有找到任何简单但有效的方法。

4

2 回答 2

1

对于毫秒使用:

timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(countup)userInfo:nil repeats:YES];

分钟使用:

timer = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector:@selector(countup)userInfo:nil repeats:YES];
于 2012-08-19T13:50:51.340 回答
0

设置更短的间隔,例如 0.1f timer = [NSTimer scheduledTimerWithTimeInterval:0.1ftarget:self selector:@selector(countup)userInfo:nil repeats:YES]; 并用于NSDateFormatter显示计数器。NSDateFormatter可以在这里找到一个示例: timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countup)userInfo:nil repeats:YES];

这里很好地解释了 NSDateFormatter 的工作原理:如何使用 NSDateFormatter 处理不同的日期时间格式

您的计时器应该更新一个浮点变量,它存储毫秒。

样本:

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"mm:ss:SSS"];
    NSTimeInterval interval = [startDate timeIntervalSinceNow]*-1;
    NSString *clock = [formatter stringFromDate:[NSDate dateWithTimeInterval:interval sinceDate:startDate]];

但是你应该去Roy Sharon 回答。

于 2012-08-19T13:48:32.387 回答