简体   繁体   English

如何在Core Animation中平滑更新动画的toValue /终点?

[英]How do I smoothly update the toValue/end point of an animation in Core Animation?

I'm trying to emulate the effect Apple has as a progress indicator when downloading an app in iOS 7, somewhat of a pie chart: 我试图模仿苹果在iOS 7中下载应用程序时作为进度指示器的效果,有点像饼图:

在此处输入图片说明

My code's coming along pretty well. 我的代码进展顺利。 I can give it a value to go to and it will update to that. 我可以给它一个值,它将更新为该值。

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    CGFloat radius = CGRectGetWidth(self.frame) / 2;
    CGFloat inset  = 1;
    CAShapeLayer *ring = [CAShapeLayer layer];
    ring.path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(self.bounds, inset, inset)cornerRadius:radius-inset].CGPath;

    ring.fillColor = [UIColor clearColor].CGColor;
    ring.strokeColor = [UIColor whiteColor].CGColor;
    ring.lineWidth = 2;

    self.innerPie = [CAShapeLayer layer];
    inset = radius/2; 
    self.innerPie.path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(self.bounds, inset, inset)
                                               cornerRadius:radius-inset].CGPath;
    self.innerPie.fillColor   = [UIColor clearColor].CGColor;
    self.innerPie.strokeColor = [UIColor whiteColor].CGColor;
    self.innerPie.lineWidth   = (radius-inset)*2;

    [self.layer addSublayer:ring];
    [self.layer addSublayer:self.innerPie];

    self.progress = 0.0;
    self.innerPie.hidden = YES;
}

- (void)setProgress:(CGFloat)progress animated:(BOOL)animated {
    if (animated) {
        self.innerPie.hidden = NO;
        self.innerPie.strokeEnd = progress;

        CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        pathAnimation.duration = 0.5;
        pathAnimation.fromValue = [NSNumber numberWithFloat:self.progress];
        pathAnimation.toValue = [NSNumber numberWithFloat:progress];

        [self.innerPie addAnimation:pathAnimation forKey:@"strokeEnd"];
    }
    else {
        [CATransaction setDisableActions:YES];
        [CATransaction begin];
        self.innerPie.hidden = NO;
        self.innerPie.strokeEnd = progress;
        [CATransaction commit];
    }

    self.progress = progress;
}

My issue lies in updating the progress while in the middle of an animation . 我的问题在于在动画过程更新进度 For example, if I'm downloading a file and the progress jumps from 0.1 to 0.2 (10% to 20%), I want to animate that. 例如,如果我正在下载文件,并且进度从0.1跳到0.2(从10%跳到20%),则我想对此进行动画处理。 Say I give it a 0.5 second animation. 假设我给了它0.5秒的动画。 What if, 0.2 seconds into the 0.5 second animation it jumps to 0.4 (40%)? 如果在0.5秒的动画中0.2秒跳到0.4(40%),该怎么办?

With my current code, it jumps to where it should have ended (when it should have animated) in the first animation (20%) and then starts animating again toward 40%. 使用我当前的代码,它会跳到第一个动画(20%)中应该结束的位置(应该动画时),然后再次朝40%的方向动画。

What I'd like is for it to almost update the toValue to 0.4. 我想要的是将它的toValue几乎更新为0.4。 At least that's what I think I want. 至少那是我想要的。 Conceptually I'd like it to continue the animation toward the new value without interrupting the previous value. 从概念上讲,我希望它在不中断先前值的情况下朝新值继续动画。 Smoothness, to put it simply. 简单地说,就是平滑度。

How would I accomplish this? 我将如何完成?

I know there's libraries to do this. 我知道有图书馆可以做到这一点。 I want to do it myself for learning purposes. 我想自己做,以学习为目的。

You should replace 你应该更换

pathAnimation.fromValue = [NSNumber numberWithFloat:self.progress];

with : 与:

pathAnimation.fromValue = [NSNumber numberWithFloat:[self.innerPie.presentationLayer strokeEnd]];

Initially, [self.innerPie.presentationLayer strokeEnd]] will be 1, so you will need to set the initial value to 0. Add these 2 lines to place where you create your 'innerPie' : 最初, [self.innerPie.presentationLayer strokeEnd]]将为1,因此您需要将初始值设置为0。将这两行添加到创建“ innerPie”的位置:

self.innerPie.strokeStart = 0;
self.innerPie.strokeEnd = 0;

I've tested and it works. 我已经测试过并且可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM