Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After rotating a CALayer using CABasicAnimation the layer jumps back to it's unrotated position

I am trying to create a falling coin. The coin image is a CALayer with 2 CABasicAnimations on it - a falling down and a rotation one. When the falling down animation gets to its end, it stays there. The rotation animation though, which is supposed to be random and end up in a different angle every time, just pops back to the original CALAyer image.

I want it to stay in the angle it finished the animation on. Is it possible? How do I do it?

Code:

//Moving down animation:
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
anim.duration = 1;
anim.autoreverses = NO;
anim.removedOnCompletion = YES;

anim.fromValue = [NSNumber numberWithInt: -80 - row_height * (8 - _col)];
anim.toValue = [NSNumber numberWithInt: 0];

//Rotation Animation:
CABasicAnimation *rota = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rota.duration = 4;
rota.autoreverses = NO;
rota.removedOnCompletion = NO;
rota.fromValue = [NSNumber numberWithFloat: 0];
rota.toValue = [NSNumber numberWithFloat: 2.5 * 3.15 ];
[cl addAnimation: rota forKey: @"rotation"];
[cl addAnimation: anim forKey: @"animateFalling"];
like image 208
Mikle Avatar asked Sep 04 '09 01:09

Mikle


3 Answers

Have you set the removedOnCompletion property of the rotation animation to NO, e.g.,

rota.removedOnCompletion = NO;

That should leave the presentation layer where it was when the animation finished. The default is YES, which will snap back to the model value, i.e., the behavior you describe.

The fillMode should also be set, i.e.,

rota.fillMode = kCAFillModeForwards;
like image 67
Allan Bazinet Avatar answered Nov 10 '22 14:11

Allan Bazinet


I was trying to rotate an arrow back and forth, like the Twitter/Facebook "Pull to Refresh" effect.

The problem is, I was doing the rotation back and forth on the same UIView, so after adding

rotation.removedOnCompletion = NO;
rotation.fillMode = kCAFillModeForwards;

The forward animation war working OK but the backwards animation was not working at all.

So I added the last line suggested by yeahdixon, and in addition set the view's transform to the animation's completed state: (rotation by 180 degrees)

[myview.layer removeAllAnimations];
myView.transform = CGAffineTransformMakeRotation(M_PI);

For the 'restore' animation (backwards) I do this on completion:

myView.transform = CGAffineTransformMakeRotation(0);

...and it works fine. Somehow it doesn't need the removeAllAnimations call.

like image 4
Nicolas Miari Avatar answered Nov 10 '22 13:11

Nicolas Miari


I found that by setting : removedOnCompletion = NO;

did not produce a visible leak in instruments, but did not get deallocated and was accumulating a small amount of memory. IDK if its my implementation or what, but by adding removeAllAnimations at the end of the animation seemed to clear out this tiny bit of residual memory.

[myview.layer removeAllAnimations];
like image 1
yeahdixon Avatar answered Nov 10 '22 13:11

yeahdixon