views:

686

answers:

2

How do you stop the rotation of a custom drawn CALayer on the iPhone?

This

CATransform3D pausetransform = ((CALayer *)[layer presentationLayer]).transform;
[layer removeAllAnimations];
layer.transform = pausetransform;

stops the animation, then immediately rotates the layer back a little bit. Probably because after the pausetransform value is set the animation continues for a short while.

Setting the layer.transform value in the animation's - (void) animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)finishednormally delegate method briefly displays the layer in the starting position of the animation before it rotates to the correct position.

A: 

Another way is to call

[view.layer removeAnimationForKey:@"name of your animation"];

However, in both removes, depending on how you set up the animation, the animation stops at the next iteration of the animation. If, let's say, you were spinning in 10 degree increments, the animation would stop before the next 10 degree increment.

mahboudz
I'm using a CABasicAnimation to rotate the layer 360° once. When I remove the animation (using removeAllAnimations or removeAnimationForKey) the layer goes back to the rotation in layer.transform. But I can't find a way of obtaining the rotation of the layer at the exact moment the animation stops. Nor am I able to set the rotation of the layer at the exact moment the animation stops. I can only do it right before or right after, both of which result in graphical glitches.Is there another subclass of CAAnimation that handles this better?
Kare Morstol
+1  A: 

It turns out the UIActionSheet I was setting up when calling [layer removeAllAnimations] took so long to display it was postponing the actual stopping of the animation. By moving the setup of the UIActionSheet to the - (void) animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)finishednormally delegate method the rotation now stops smoothly.

Kare Morstol