I have one like this:
CALayer *layer = stripeButton.layer;
CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"bounds.origin.y"];
moveAnim.duration = 3.35;
moveAnim.repeatCount = 0;
moveAnim.autoreverses = NO;
moveAnim.removedOnCompletion = NO;
moveAnim.calculationMode = kCAAnimationLinear;
moveAnim.fillMode = kCAFillModeForwards;
// ... keys and values here ...
[theLayer addAnimation:moveAnim forKey:@"theAnimation"];
You see this animation goes 3.35 seconds. In the meantime, it can happen that at i.e. 2 seconds a new animation with the same key @"theAnimation" is kicked off, for different values.
Problem what's happening: New animation does not pick up the current visible state. It begins hard from scratch. The view jumps to start position in a ugly manner and from there, the new animation begins. For UIView there is an setAnimationsBeginFromCurrentState=YES, but I haven't found anything like that for CAKeyFrameAnimation. Any idea?
You want to query the layer for its presentation layer, which is its current interpolated state including any animations currently running.
-
Excerpt from the CALayer Class Reference:
-(id)presentationLayer
Returns a copy of the layer containing all properties as they were at the start of the current transaction, with any active animations applied.
-
The relationship of the Layer Tree (your data), the Presentation Tree (current interpolation values of your data), and the Render Tree are explained here in the CA programming guide.