在标签中显示计时器

isc*_*ers 9 iphone

和大多数游戏一样,我以"01:05"的格式看过计时器

我试图实现一个计时器,并在重置时我需要将计时器重置为"00:00".

此计时器值应在标签中.

如何创建一个递增的计时器?比如00:00 --- 00:01 --- 00:02 ..........像dat一样的东西.

建议

问候

Hec*_*204 25

我用过的一个简单方法就是:

//In Header
int timeSec = 0;
int timeMin = 0;
NSTimer *timer;

//Call This to Start timer, will tick every second
-(void) StartTimer
{
     timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
     [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

//Event called every time the NSTimer ticks.
- (void)timerTick:(NSTimer *)timer
{
     timeSec++;
     if (timeSec == 60)
     {
        timeSec = 0;
        timeMin++;
     }
     //Format the string 00:00
     NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
     //Display on your label
     //[timeLabel setStringValue:timeNow];
         timeLabel.text= timeNow;
}

//Call this to stop the timer event(could use as a 'Pause' or 'Reset')
- (void) StopTimer
{
    [timer invalidate];
    timeSec = 0; 
    timeMin = 0;
     //Since we reset here, and timerTick won't update your label again, we need to refresh it again.
     //Format the string in 00:00
     NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
     //Display on your label
// [timeLabel setStringValue:timeNow];
       timeLabel.text= timeNow;
}
Run Code Online (Sandbox Code Playgroud)