views:

1159

answers:

6

I'm working on an animated clock application for the iPhone, and I have to pivot all the 3 nodes in the view, which I have obtained in the following code:

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];

clockarm.layer.anchorPoint = CGPointMake(0.0, 0.0);
[CATransaction commit];

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanFalse forKey:kCATransactionDisableActions];
[CATransaction setValue:[NSNumber numberWithFloat:50.0] forKey:kCATransactionAnimationDuration];

CABasicAnimation *animation;
animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:-60.0];
animation.toValue = [NSNumber numberWithFloat:2 * M_PI];
animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
animation.delegate = self;
[clockarm.layer addAnimation:animation forKey:@"rotationAnimation"];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];


[CATransaction commit];

The problem it's just rotating once, ie. only 360 degree and then stopping. I want to raotate the needles indefinitely. How would I do that?

+2  A: 

Add the animationDidFinish delegate method and setup the animation again for another rotation. CA wont let you turn things more than 360 degrees.

Squeegy
+4  A: 

I've never done it, but here's what I would try. You've already set the delegate to self. Implement the delegate's method animationDidStop:finished: and simply call the method where you'll put the code above. Make sure to call it on the main thread (using performOnMainThread).

François P.
There's no reason to keep adding the same animation each time it finishes. That's the point of repeatCount in an animation. Core Animation can handle continuous rotation without resorting to waiting for the animation to stop. See Brad Larson's answer. It makes a lot more sense.
Matt Long
+2  A: 

This is related to the question asked here, where I pointed out my response to a similar question. In that case, I split the rotation animations into half-circle rotations, made them cumulative, and set the number of half rotations as the repeat count. As I suggest in both, doing this as a CAKeyframeAnimation might produce a cleaner rotation.

In your case, you could set the number of repetitions to a high enough value that the animation would never reach its end. As a fallback, you could follow François P.'s and Squeegy's suggestion and add a callback to restart the animation if it ever completes.

Brad Larson
A: 

I use "scalingAnimation.repeatCount=1000000000;" for the purpose. Not sure if it's the way for you.

slatvick
+1  A: 

I would look at the problem slightly differently. I'm not sure why you're using 50 seconds for the time, but I wouldn't rely on this to keep accurate time. I would instead set a timer which fires every second, or half second, and animates the clock to move from it's current position to the new time.

P.S. Your fromValue looks to be in degrees, but it should be radians.

David Kanarek
A: 

for n no. of repetition try to use value -1, i never do this but may it will help u.