加载视图控制器后如何使用计时器设置进度条

How to set progress bar With timer after view controller loaded

我在循环进度条上使用计时器并在界面中使用 NSDate 属性(测试也使用 viewWillAppear)但是当 viewController 加载时它显示从 27 秒开始剩余,而我已经设置其值为 30 秒。

我的情况是,用户有一个问题和可选答案,并且 he/she 有一些时间,假设 he/she 单击开始按钮时 30 秒,然后另一个屏幕在该屏幕加载后打开,开始时间应该是从 30 秒开始。

@interface TestViewController ()

 {
    NSTimeInterval totalCountdownInterval;
    NSDate* startDate;
 }
}

-(void)viewWillAppear:(BOOL)animated{

    [self startTimer];
    totalCountdownInterval = 30.0;
    startDate = [NSDate date];
}

-(void)startTimer {

      if (!_timer) {
          _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                  target:self
                                                selector:@selector(timerFired:)
                                                userInfo:nil
                                               repeats:YES];
    }
}

-(void)stopTimer {

      if ([_timer isValid]) {
          [_timer invalidate];
      }
      _timer = nil;
}

-(void)timerFired:(NSTimer *)timer {

    NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:startDate];
    NSTimeInterval remainingTime = totalCountdownInterval - elapsedTime;

    CGFloat timeRemain = remainingTime;
    NSLog(@"%f", timeRemain);
    if (remainingTime < 0.0) {
        [_timer invalidate];
    }

    [_circleProgressBar setHintTextGenerationBlock:(^NSString *(CGFloat progress) {

        if (!(timeRemain < 0.9)) {
            progress = timeRemain - 1;
            return [NSString stringWithFormat:@"%.0f", progress];
        }else{
            progress = 0;
            return [NSString stringWithFormat:@"%.0f", progress];
        }

    })];
    [_circleProgressBar setProgress:(_circleProgressBar.progress + 0.0333f) animated:YES];
}

-(void) viewWillDisappear:(BOOL)animated {
    [self stopTimer];
}

如有任何帮助,我们将不胜感激。谢谢

使用void视图中的计时器代码确实出现并尝试了一次。

- (void)viewDidAppear:(BOOL)animated
{

[self startTimer];
totalCountdownInterval = 30.0;
startDate = [NSDate date];
}

第一个计时器事件将在 1 秒后触发,因此您应该首先看到 29。所以你应该在计时器事件之外以某种方式手动显示数字 30。

并且您在 setHintTextGenerationBlock 的代码中减去 1: 进度 = 剩余时间 - 1; 这会让你看到 28 作为初始值。

最后,您应该在 viewDidAppear 中启动计时器,而不是在 viewWillAppear 中启动计时器,这样您又会失去一秒钟并达到 27。

正确的代码应该是这样的:

@interface TestViewController ()

 {
    NSTimeInterval totalCountdownInterval;
    NSDate* startDate;
 }
}

-(void)viewDidLoad {
    [_circleProgressBar setHintTextGenerationBlock:(^NSString *(CGFloat progress) {

        return [NSString stringWithFormat:@"%.0f", progress * 30];

    })];
    [_circleProgressBar setProgress:1.0) animated:NO];
}

-(void)viewDidAppear:(BOOL)animated{

    [self startTimer];
    totalCountdownInterval = 30.0;
    startDate = [NSDate date];
}

-(void)startTimer {

      if (!_timer) {
          _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                                  target:self
                                                selector:@selector(timerFired:)
                                                userInfo:nil
                                               repeats:YES];
    }
}

-(void)stopTimer {

      if ([_timer isValid]) {
          [_timer invalidate];
      }
      _timer = nil;
}

-(void)timerFired:(NSTimer *)timer {

    NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:startDate];
    NSTimeInterval remainingTime = totalCountdownInterval - elapsedTime;

    CGFloat timeRemain = remainingTime;
    NSLog(@"%f", timeRemain);
    if (remainingTime < 0.0) {
        [_timer invalidate];
    }

    [_circleProgressBar setProgress:(1.0 * timeRemain / 30.0) animated:YES];
}

-(void) viewWillDisappear:(BOOL)animated {
    [self stopTimer];
}

您应该在开始时将进度设置为 1.0,并在 30 步后倒数到 0.0。 设置提示生成块以显示对 1.0 - 0.0 有意义的内容为 30 - 0。