倒数计时器从4开始

时间:2012-11-16 11:20:18

标签: iphone ios ios5 ios6 ios-simulator

当我开始进行测验时,计时器开始倒计时“4 ... 3 ... 2 ... 1 ...”。 我想在第一个测验问题中添加这个倒计时。倒计时后显示第一个问题。

任何人都可以帮助我

我试图这样做,但我没有获得倒计时的格式。

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;
}

1 个答案:

答案 0 :(得分:0)

你可以这样做

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];

}