CABasicAnimation - 从一个动画切换到另一个动画。如何获得动画的当前值?

问题描述:

嗨我正在使用CABasicAnimation在CAShapeLayer中设置动画路径。当用户TAPP的按钮,我叫:CABasicAnimation - 从一个动画切换到另一个动画。如何获得动画的当前值?

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的值。

+0

你尝试'[层removeAnimationForKey:@ “路径”];'做一个动画之前? – anhtu

+0

这不是我想的问题。当我调用第二个动画时,第一个动画会自动停止。我只想让这个圈子从当前状态开始缩小,而不是从最大尺寸缩小。 –

+0

谢谢,我现在明白了这个问题。 – anhtu

您可以使用presentationLayer,它返回表示当前出现在屏幕上的图层状态的表示层对象的副本。

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

编辑: 所以解决的办法是

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

我认为这是要走的路。但我怎样才能访问路径属性? –

+0

hm ..你可以得到它的位置,并从那个位置开始第二个动画 – l0gg3r

+0

CAShapeLayer有一个'path'属性,是不是你需要的? – l0gg3r