您好,我试图制作一个简单的倒计时应用。
然而,我的倒计时只有几秒钟,而不是几分钟和几小时。我怎么做到这一点?
答案 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];
}
答案 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];
}