【发布时间】:2016-11-22 11:46:36
【问题描述】:
我正在使用nstimer 在标签中显示倒数计时器。我可以启动计时器并在标签中显示倒计时,但计时器会跳到下一秒而不是每秒显示一次。如果倒数计时器设置为 10 秒,则倒数计时器标签中仅显示 9、7、5、3、1。
下面是我的代码。
NSTimer *tktTimer;
int secondsLeft;
- (void)startTimer {
secondsLeft = 10;
tktTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCountdown) userInfo:nil repeats: YES];
}
-(void) updateCountdown {
int hours, minutes, seconds;
secondsLeft--;
NSLog(@"secondsLeft %d",secondsLeft);//every time it is printing 9,7,5,3,1 but should print 9,8,7,6,5,4,3,2,1,0
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
countDownlabel.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
if (--secondsLeft == 0) {
[tktTimer invalidate];
countDownlabel.text = @"Completed";
}
}
任何帮助将不胜感激。
【问题讨论】:
标签: ios objective-c iphone xcode nstimer