与NSTimer一起使用时,进度条显示滴答动画而不是平滑动画



我使用NSTimer和圆形进度条作为倒数计时器,即15秒

它的工作与以下代码,但我得到tick动画的进度条,而不是光滑的一个,如何使它平滑的动画

- (void)viewDidLoad
        {
            [super viewDidLoad];
           self.labeledLargeProgressView.roundedCorners = NO;
            self.labeledLargeProgressView.trackTintColor =[UIColor colorWithRed:0.0f/255.0f       green:173.0f/255.0f blue:255.0f/255.0f alpha:1.0f];
            self.labeledLargeProgressView.progressTintColor =[UIColor colorWithRed:255.0f/255.0f    green:96.0f/255.0f blue:88.0f/255.0f alpha:1.0f];
            self.labeledLargeProgressView.thicknessRatio = 1.0f;
            self.labeledLargeProgressView.clockwiseProgress = YES;
            [self.view addSubview:self.labeledLargeProgressView];
            seconds = 15.0;
            [self startAnimation];
        }
- (void)progressChange
{
    CGFloat progress ;
    DALabeledCircularProgressView *labeledProgressView = self.labeledLargeProgressView;
    if(labeledProgressView.progress >=1.0f && [self.timer isValid]){
        [self stopAnimation];
         seconds = 15.0f;
    }
    else{
        progress=labeledProgressView.progress + 0.06666667f;
        [labeledProgressView setProgress:progress animated:YES];
        seconds --;
        labeledProgressView.progressLabel.text = [NSString stringWithFormat:@"%i", seconds];
    }

}
- (void)startAnimation
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                  target:self
                                                selector:@selector(progressChange)
                                                userInfo:nil
                                                 repeats:YES];
    self.continuousSwitch.on = YES;
}
- (void)stopAnimation
{
    [self.timer invalidate];
    self.timer = nil;
    self.continuousSwitch.on = NO;
}

我发现的是一组UIview动画和进度视图动画,工作得更好,动画的进度视图顺利。如果你只是使用动画和progressview动画的组合,它会直接触发,而不考虑UIview动画计时器。

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    timer = [NSTimer scheduledTimerWithTimeInterval: 1.0f
                                             target: self
                                           selector: @selector(updateTimer)
                                           userInfo: nil
                                            repeats: YES];
}

- (void)updateTimer
{
    if (progressView.progress >= 1.0) {
        [timer invalidate];
    }
    [UIView animateWithDuration:1 animations:^{
        float newProgress = [self.progressView progress] + 0.125;
        [self.progressView setProgress:newProgress animated:YES];
    }];
}

可以随意调整动画时间,以实现更好更流畅的过渡

我自己解决了这个问题,我做的是更频繁地更新0.1f而不是1.0f

- (void)progressChange
{
    CGFloat progress ;
    DALabeledCircularProgressView *labeledProgressView = self.labeledLargeProgressView;
    if(labeledProgressView.progress >=1.0f && [self.timer isValid]){
        [self stopAnimation];
         seconds = 15.0f;
        _counter = 0;
    }
    else{
        progress=labeledProgressView.progress + 0.00666667f;
        _counter ++;
        [labeledProgressView setProgress:progress animated:YES];
        if(_counter % 10 == 0){
            seconds --;
        }
        labeledProgressView.progressLabel.text = [NSString stringWithFormat:@"%i", seconds];
    }

}

- (void)startAnimation
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1
                                                  target:self
                                                selector:@selector(progressChange)
                                                userInfo:nil
                                                 repeats:YES];
    self.continuousSwitch.on = YES;
}

最新更新