简体   繁体   中英

Countdown Timer starts from 4 to 1

The timer starts a countdown of “4…3…2…1…” when I start to play the Quiz. I want to add this countdown in first question of quiz. After countdown the first question display.

Can any one help me please

I m trying to do this but i did'nt get format for countdown.

NSTimer *timer_total = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];

- (void)updateTimer
{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate_total];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    self.df_total = [[NSDateFormatter alloc] init];
    [self.df_total  setDateFormat:@"mm:ss"];
    [self.df_total setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    NSString *timeString=[self.df_total stringFromDate:timerDate];
    self.lbl_total_Timer.text = timeString;
}

you can do like

int currentTime;
- (IBAction)start{

    currentTime = 5;
    lbl=[[UILabel alloc]init];
    //creates and fires timer every second
    myTimer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showTime) userInfo:nil repeats:YES]retain];
}

-(void)showTime{


    if(currentTime==0)
    {
        [myTimer invalidate];
        myTimer = nil;
    }
    else{


    currentTime--;
    lbl.text = [NSString stringWithFormat:@"%.2d", currentTime];
    }
    NSLog(@"my lable == %@",lbl.text);


}

- (void)viewWillAppear:(BOOL)animated 
{
    [self start];
    [super viewWillAppear:YES];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM