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

时间:2014-05-11 08:02:34

标签: ios objective-c nstimer uiprogressview uiprogressbar

我正在使用NSTimer和循环进度条作为倒数计时器,即15秒

它使用以下代码,但我得到了进度条的刻度动画,而不是平滑的动画,如何使其平滑动画

- (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;
}

2 个答案:

答案 0 :(得分:1)

我发现的是一组UIview动画和进度视图动画,它们运行得更好,并且可以顺利地为进度视图制作动画。如果您只使用动画和进度视图动画的组合,则会直接触发,而不考虑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];
    }];
}

随意调整动画时间以更好地平滑过渡

答案 1 :(得分:0)

我自己解决了我所做的是我更频繁地更新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;
}
相关问题