2010-09-25 9 views
9

Como en la mayoría de los juegos i hv visto el temporizador en un formato "01:05"programa de temporizador en la etiqueta

que estoy tratando de implementar un temporizador, y el restablecimiento necesito para restablecer el temporizador para "00:00 ".

valor Este temporizador debe estar en la etiqueta.

Cómo crear un temporizador que está incrementando? como 00:00 --- 00:01 --- 00:02 .......... algo así como dat.

sugerencias

respecto

Respuesta

25

una forma sencilla que he utilizado es la siguiente:

//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; 
} 
+0

gracias a Héctor para responder. recibo un error en "NSEventTrackingRunLoopMode" ... ¿cómo definir esto? – iscavengers

+0

Intente cambiarlo a: [[NSRunLoop currentRunLoop] addTimer: timer forMode: NSDefaultRunLoopMode]; – Hector204

+0

bien lo intentará ...... – iscavengers

1

crear un objeto de tipo NSDate e.g.'now'. Después de eso, siga el siguiente código:

self.now = [NSDate DAte]; 
long diff = -((long)[self.now timeIntervalSinceNow]); 
timrLabel.text = [NSString stringWithFormat:@"%02d:%02d",(diff/60)%60,diff%60]; 
+0

esto le dará visualización solo –

4

Esto debería darle un temporizador que es bastante preciso, al menos a simple vista. Esto no se actualiza la vista, sólo se actualiza la hora y la etiqueta:

//call this on reset. Take note that this timers cannot be used as absolutely correct timer like a watch for example. There are some variation. 

-(void) StartTimer 
{ 
    self.startTime = [NSDate date] //start dateTime for your timer, ensure that date format is correct 
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES]; 
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; 
} 


-(void) timerTick 
{ 
    NSTimeInterval timeInterval = fabs([self.startTime timeIntervalSinceNow]); //get the elapsed time and convert from negative value 
    int duration = (int)timeInterval; // cast timeInterval to int - note: some precision might be lost 
    int minutes = duration/60; //get the elapsed minutes 
    int seconds = duration % 60; //get the elapsed seconds 
    NSString *elapsedTime = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds]; //create a string of the elapsed time in xx:xx format for example 01:15 as 1 minute 15 seconds 
    self.yourLabel.text = elapsedTime; //set the label with the time 
} 
0

Temporizador con pausa (en base a la respuesta Hector204):

UILabel *timerLabel; //label where time will be showed. 
#define kTimerOff 0 
#define kTimerOn 1 

-(void)startTimer:(UILabel*)timerLabel 
{ 
    if (timerLabel.tag == kTimerOn) 
     [self stopTimer:timerLabel]; 
    else 
    { 
     timerLabel.tag = kTimerOn; 

     self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:timerLabel repeats:YES]; 
     [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode]; 
    } 
} 

-(void)stopTimer:(UILabel*)timerLabel 
{ 
    timerLabel.tag = kTimerOff; 

    [self.timer invalidate]; 
} 


-(void)timerTick:(NSTimer *)timer 
{ 
    UILabel *currentTimerLabel = timer.userInfo; 
    NSString *currentTimerString = currentTimerLabel.text; 
    int minutes = [[currentTimerString substringToIndex:2] intValue]; 
    int seconds = [[currentTimerString substringFromIndex:3] intValue]; 

    seconds++; 
    if (seconds == 60) 
    { 
     seconds = 0; 
     minutes++; 
    } 
    currentTimerLabel.text = [NSString stringWithFormat:@"%02d:%02d", minutes, seconds]; 
}