In theory this progress bar should last 7 seconds, however it seems to run a little long. My math has to be incorrect or I'm overlooking something.

Timer should be firing 100 times in 1 second and the progress bar should take about 7 times longer than that to reach 1.0

Any help would be greatly appreciated.

- (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];
    }
}
有帮助吗?

解决方案

You should not update as often as you do. 100 times per second is way too often. 60 would be sufficient to achieve a good frame-rate in theory. However a UIProgressBar can update its value with an animation. Hence you only need to update say 70 times in total or 10 times per second and make the changes with an animation.

I would go for 10 animated updates or less. Or you could try to update with one animation:

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

While I did not test this approach, it seems far cleaner than manually performing what essentially is a timed animation of the progress.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top