I am trying to move a CALayer a few points every time a user does something. I setup the animation to run relatively slowly but when I set the position of the layer after setting up the animation the implicit animation seems to take over and perform the animation quickly. Here is basically what my code looks like:
CGPoint point = CGPointMake(layer.position.x + 30, layer.position.y + 30);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [NSValue valueWithCGPoint: layer.position];
animation.toValue = [NSValue valueWithCGPoint: point];
animation.duration = 4;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[layer addAnimation:animation forKey:@"position"];
layer.position = point;
I have tried removing the last layer.position = point
line which results in the first animation running as expected, but then when I try to run the animation a second time the layer jumps back to where the layer was before I ran the first animation. Currently the only way I have been able to resolve this is by adding a delegate to the animation that implements animationDidStop
and setting the layer's point after the animation is completed. But looking at a lot of other people's sample code this should not be required. Is there something I am doing wrong?
removedOnCompletion = NO
and you don't show the code to remove the animation from the layer... – trojanfoelayer
? could you show how you initialize thelayer
variable? besides this: I suppose that if you useremovedOnCompletion = YES
, then the layer goes back to the initial position right after the animation completion, does it? – sergioremovedOnCompletion = YES
or removeanimation.fillMode = kCAFillModeForwards
then the layer snaps back to its original position after the animation is complete. My goal is the progressively to move the layer every time the animation is run. – Yanamon