6

我正在开发一个 QuiZ 应用程序。它需要一个倒数计时器,因为我想在 75:00.00 (mm:ss.SS) 启动一个计时器,并通过减少 1 毫秒将其减少到 00:00.00 (mm:ss.SS)。我想显示 Time UP 的警报!当时间到达 00:00.00 (mm:ss.SS)。

我通过以下链接显示时间

使用 NSTimer 的秒表错误地在显示中包含暂停时间

4

2 回答 2

13

这是您问题的简单解决方案。

声明

@interface ViewController : UIViewController
{
    IBOutlet UILabel * result;      
    NSTimer * timer;                
    int currentTime;
}

- (void)populateLabelwithTime:(int)milliseconds;
-(IBAction)start;
-(IBAction)pause;
@end

定义

- (void)viewDidLoad
{
    [super viewDidLoad];

    currentTime = 270000000; // Since 75 hours = 270000000 milli seconds


}

-(IBAction)start{
   timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];

}
-(IBAction)pause{
    [timer invalidate];

}
- (void)updateTimer:(NSTimer *)timer {
    currentTime -= 10 ;
    [self populateLabelwithTime:currentTime];
     }
- (void)populateLabelwithTime:(int)milliseconds {
    int seconds = milliseconds/1000;
    int minutes = seconds / 60;
    int hours = minutes / 60;

    seconds -= minutes * 60;
    minutes -= hours * 60;

   NSString * result1 = [NSString stringWithFormat:@"%@%02dh:%02dm:%02ds:%02dms", (milliseconds<0?@"-":@""), hours, minutes, seconds,milliseconds%1000];
    result.text = result1;

}
于 2012-04-21T09:04:51.793 回答
3

使用以下代码减少计时器。

double dblRemainingTime=60; //1 minute

if(dblRemainingTime >0)
{
     dblRemainingTime -= 1;
     int hours,minutes, seconds;
     hours = dblRemainingTime / 3600;
     minutes = (dblRemainingTime - (hours*3600)) / 60;
     seconds = fmod(dblRemainingTime, 60);  
     [lblRemainingTime setText:[NSString stringWithFormat:@"%02d:%02d",minutes, seconds]];
}
else
      alert('Time up buddy');
于 2012-04-21T08:14:19.150 回答