【问题标题】:NSTimer countdown skips last second (not using last milliseconds)NSTimer 倒计时跳过最后一秒(不使用最后毫秒)
【发布时间】:2013-09-27 19:39:21
【问题描述】:

感谢您的回答,我有一个 NS 计时器倒计时,它工作得非常好。但是,我的计时器跳过了最后一秒,因此它不计算最后的 0s:99 ms 。我的代码有什么问题吗?最好的问候!

-(void) setTimer {
    MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
    tmp.milisecondsCount = 99;
    tmp.secondsCount = 2;



    tmp.countdownTimerGame = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];


}

-(void) timerRun {
    MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
    tmp.milisecondsCount = tmp.milisecondsCount - 1;



    if(tmp.milisecondsCount == 0){
        tmp.secondsCount -= 1;


        if (tmp.secondsCount == 0){

            //Stuff for when the timer reaches 0

            [tmp.countdownTimerGame invalidate];
            tmp.countdownTimerGame = nil;
            tmp.lives = tmp.lives - 1;
            NSString *newLivesOutput = [NSString stringWithFormat:@"%d", tmp.lives];
            livesLabel.text = newLivesOutput;
            if (tmp.lives == 0) {
                timeLabel.text = @"0:00";
                [self performSelector:@selector(stopped) withObject:nil afterDelay:2.0];

            }
            else {[self setTimer]; }
        }
        else

            tmp.milisecondsCount = 99;
    }


    NSString *timerOutput = [NSString stringWithFormat:@"%2d:%2d", tmp.secondsCount, tmp.milisecondsCount];

    timeLabel.text = timerOutput;






}

【问题讨论】:

    标签: iphone ios xcode nstimer countdown


    【解决方案1】:

    尝试改变这一点

    -(void) setTimer {
        MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
        tmp.milisecondsCount = 100;// Change this from 99 to 100
        tmp.secondsCount = 2;
    
    
    
        tmp.countdownTimerGame = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(timerRun) userInfo:nil repeats:YES];
    
    
    }
    

    【讨论】:

    • 我已经试过了。但它不起作用。无论如何感谢您的回答。
    【解决方案2】:

    首先,摆脱

    else {[self setTimer]; }
    

    否则,每次选择器触发时,您都会重新启动计时器。

    下一步:交换这两个

        tmp.secondsCount -= 1;
    
        if (tmp.secondsCount == 0){
    

    else {
        tmp.secondsCount -= 1;
        tmp.milisecondsCount = 99;
    }
    

    像这样:

    -(void) timerRun {
        MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
        tmp.milisecondsCount = tmp.milisecondsCount - 1;
    
        if(tmp.milisecondsCount == 0){
    
            if (tmp.secondsCount == 0){
    
                //Stuff for when the timer reaches 0
    
                [tmp.countdownTimerGame invalidate];
                tmp.countdownTimerGame = nil;
                tmp.lives = tmp.lives - 1;
                NSString *newLivesOutput = [NSString stringWithFormat:@"%d", tmp.lives];
                livesLabel.text = newLivesOutput;
                if (tmp.lives == 0) {
                    timeLabel.text = @"0:00";
                    [self performSelector:@selector(stopped) withObject:nil afterDelay:2.0];
    
                }
            }
            else
            {
                tmp.milisecondsCount = 99;
                tmp.secondsCount -= 1;
            }
        }
    
        NSString *timerOutput = [NSString stringWithFormat:@"%2d:%2d", tmp.secondsCount, tmp.milisecondsCount];
    
        timeLabel.text = timerOutput;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多