如何将毫秒添加到秒表应用程序?

好的,所以我在这里基于我的秒表应用程序代码http://iphonedev.tv/blog/2013/7/7/getting-started-part-3-adding-a-stopwatch-with-nstimer-我们的头等舱我喜欢它的设置方式,但是我不知道如何给它增加百分之一秒,任何人都知道如何做到这一点?

我的ViewController.m文件

#import "ViewController.h" #import "Foundation/Foundation.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (NSTimer *)createTimer { return [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerTicked:) userInfo:nil repeats:YES]; } - (void)timerTicked:(NSTimer *)timer { _currentTimeInSeconds++; self.timeLabel.text = [self formattedTime:_currentTimeInSeconds]; } - (NSString *)formattedTime:(int)totalSeconds { int hundredths = totalSeconds % 60; int seconds = totalSeconds % 60; int minutes = (totalSeconds / 60) % 60; int hours = totalSeconds / 3600; return [NSString stringWithFormat:@"%02d:%02d:%02d.%02d", hours, minutes, seconds, hundredths]; } - (IBAction)startButtonPressed:(id)sender { if (!_currentTimeInSeconds) { _currentTimeInSeconds = 0 ; } if (!_theTimer) { _theTimer = [self createTimer]; } } - (IBAction)stopButtonPressed:(id)sender { [_theTimer invalidate]; } - (IBAction)resetButtonPressed:(id)sender { if (_theTimer) { [_theTimer invalidate]; _theTimer = [self createTimer]; } _currentTimeInSeconds = 0; self.timeLabel.text = [self formattedTime:_currentTimeInSeconds]; } @end 

再次感谢任何可以帮助的人!

首先,你应该把你的variables的名字从_currentTimeInSeconds改为_currentTimeInHundredths (或者如果你想要更短的话)。

接下来,您需要更新- (NSString *)formattedTime:(int)totalSeconds方法中的逻辑。 尝试这样的事情(与以前一样,将totalSeconds改为totalHundredths)。

 int hours = totalHundredths / 360000; int minutes = (totalHundredths - (hours * 360000)) / 6000; int seconds = (totalHundredths - (hours * 360000) - (minutes * 6000)) / 100; int hundredths = totalHundredths - (hours * 360000) - (minutes * 6000) - (seconds * 100); 

我没有testing数字的math,但他们应该是正确的。