CABasicAnimation - 从一个动画切换到另一个。如何获取动画的currentValue?

CABasicAnimation - switch from one animation to another. How to get currentValue of animation?

您好,我正在使用 CABasicAnimation 为 CAShapeLayer 中的路径设置动画。当用户点击我调用的按钮时:

CABasicAnimation *expandAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    expandAnimation.toValue = (id)[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,30,30)].CGPath;
    expandAnimation.duration = 1.0;
    expandAnimation.fillMode = kCAFillModeBoth;
    expandAnimation.removedOnCompletion = NO;
    [self.circleShape addAnimation:expandAnimation forKey:expandAnimation.keyPath];

当释放按钮时,我将其设置为动画:

 CABasicAnimation *narrowAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    narrowAnimation.fromValue = (id)[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,30,30)].CGPath;
    narrowAnimation.toValue = (id)[UIBezierPath bezierPathWithOvalInRect:CGRectMake(5, 5, 20, 20)].CGPath;
    narrowAnimation.duration = .5;
    narrowAnimation.fillMode = kCAFillModeBoth;
    narrowAnimation.removedOnCompletion = NO;
    [self.circleShape addAnimation:narrowAnimation forKey:narrowAnimation.keyPath];

有时在我启动第二个动画时第一个动画仍在播放。我究竟如何才能获得第一个动画的当前状态,以便我可以将第二个动画的起始值设置为从该状态开始?

对于我使用 BezierPaths 的值。

您可以使用 presentationLayer,其中 returns 表示层当前显示在屏幕上的状态的表示层对象的副本。

因此,通过使用self.circleShape.presentationLayer,您将获得当前显示在屏幕上的图层。

编辑: 所以解决方案是

narrowAnimation.fromValue = [self.circleShape.presentationLayer valueForKeyPath:@"path"];