views:

280

answers:

3
+1  Q: 

animate rotation

I'm trying to animate a rotation using CATransform3DMakeRotation, but the problem is once the animation is finished, the image goes back to its initial position, i.e back to zero. But I'd like to keep it where it finished rotating. How would I do that?

edit What I'm trying to do is to create the same compass which comes with the new iPhone. Basically the locationmanager gives me new headings every few seconds (or several per second). Using the new heading and the timestamp, I was trying to get a smooth animation of the image but not getting anywhere. The only thing which seems to work is applying the transform directly, e.g.

compassimage.layer.transform = CATransform3DMakeRotation(newHeading.trueHeading *M_PI/180,0,0,1.0):

but that's not animated...

A: 

You should set the animation.removedOncompletion to NO

Remus Rusanu
Thanks, that seems to do something but still not getting there. I'm going to extend the problem description above, could you please have a look there?
A: 

Ok, back to basics, just trying to rotate an image by 90 degrees and keeping it there. Added the following code to - (void)viewDidLoad but even though it rotates it still going back to it's initial position once the animation is finished

CATransform3D rotationTransform = CATransform3DMakeRotation(M_PI, 0, 0, 1.0);

CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
rotationAnimation.duration = 2.0f;
rotationAnimation.removedOnCompletion = NO;

[image.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
I'm doing very similar but I also add a delegate to self, implement animationDidStop: finished:. In the delegate I remove the animation and set the layer transform to the finished value (the last value of the animation.values).
Remus Rusanu
A: 

all you have to do is to add change the figure to negative.

compassimage.layer.transform = CATransform3DMakeRotation(-newHeading.trueHeading *M_PI/180,0,0,1.0):