简单的NSTimer进度条7秒

Sam*_*Sam 0 progress nstimer ios

从理论上讲,这个进度条应该持续7秒,但它看起来有点长.我的数学必须是不正确的,否则我会忽略一些东西.

计时器应该在1秒内触发100次,进度条应该比达到1.0的时间长7倍

任何帮助将不胜感激.

- (void)startTimer;
{
    [pBar showProgress:self.progress];

    [self.timer invalidate];
    self.timer = nil;

    if (self.progress < 1.0) {
        CGFloat step = 0.01;
        self.timer = [NSTimer scheduledTimerWithTimeInterval:step target:self
                                                selector:@selector(startTimer)
                                                userInfo:nil repeats:NO];
        self.progress = self.progress + 0.00143;
    } else {
        [self performSelector:@selector(stopProgress)
               withObject:nil afterDelay:0.5];
    }
}
Run Code Online (Sandbox Code Playgroud)

Fel*_*oux 5

您不应该像经常更新.每秒100次太频繁了.在理论上,60足以实现良好的帧速率.但是,UIProgressBar可以使用动画更新其值.因此,您只需要更新总共70次或每秒10次,并使用动画进行更改.

我会去10个或更少的动画更新.或者您可以尝试使用一个动画进行更新:

[UIView animateWithDuration:7.0 animations:^{
    [progressView setProgress:1.0 animated:YES]; //maybe try animated: NO here
} completion:^(BOOL finished) {
    //ended
}];
Run Code Online (Sandbox Code Playgroud)

虽然我没有测试这种方法,但它似乎比手动执行本质上是进度的定时动画更清晰.