简单的NSTimer进度条7秒

时间:2013-12-10 08:43:06

标签: ios nstimer progress

理论上,这个进度条应持续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];
    }
}

1 个答案:

答案 0 :(得分: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
}];

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

相关问题