如何在NSTimer中显示hh:mm:ss?

时间:2014-04-22 10:12:19

标签: ios timer nstimer

您好,我试图制作一个简单的倒计时应用。

然而,我的倒计时只有几秒钟,而不是几分钟和几小时。我怎么做到这一点?

3 个答案:

答案 0 :(得分:1)

您需要使用一些逻辑,将秒数转换为分钟,小时。

让我们说:

s秒。

h = s/3600;
m = s/60-h*60;
s = s%60;
NSString *clockTime = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",h,m,s];

然后,您可以使用所需的hh:mm:ss

形成一个字符串

答案 1 :(得分:0)

创建计时器:

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

倒计时方法:

-(void) Countdown {
    int hours, minutes, seconds;

    secondsLeft--;
    hours = secondsLeft / 3600;
    minutes = (secondsLeft % 3600) / 60;
    seconds = (secondsLeft %3600) % 60;
    label.text = [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds];
}

参考:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Timers/Articles/usingTimers.html

答案 2 :(得分:0)

以下是您的答案: -

- (void)displayLabel:(UILabel *)lbl timerInterval:(NSTimeInterval)timerInterval {
 uint seconds = fabs(timerInterval); 
 uint minutes = seconds / 60;
 uint hours = minutes / 60;
 seconds -= minutes * 60;
 minutes -= hours * 60;

 lbl.text=[NSString stringWithFormat:@"%@%02uh:%02um:%02us", (timerInterval<0?@"-":@""),         hours, minutes, seconds];
}