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

时间:2018-03-20 06:57:49

标签: ios objective-c iphone nsdate nstimer

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

我的情况是,用户有一个问题和可选的答案,并且他/她有一些时间假设他/她点击开始按钮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];
}

任何帮助将不胜感激。感谢。

2 个答案:

答案 0 :(得分:1)

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

- (void)viewDidAppear:(BOOL)animated
{

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

答案 1 :(得分:1)

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

你在setHintTextGenerationBlock中的代码中减去1: progress = timeRemain - 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的有意义的内容。