PDA

View Full Version : Using CABasicAnimation with sleep()




kimicnoo
Jul 19, 2009, 11:42 PM
Hi All,

I meet one problem while I implement my first game using CABasicAnimation.
I want following sequence..
(1) do animation (rotation)
(2) wait(sleep) 2 sec.

But, When I run below source.. It works like following sequence
(1) wait(sleep) 2sec.
(2) do animation (rotation)


Plesase refer to my source..


// create rotation animation around z axis
CABasicAnimation *rotateAnimation = [CABasicAnimation animation];
rotateAnimation.keyPath = @"transform.rotation.z";
rotateAnimation.fromValue = [NSNumber numberWithFloat:DegreesToRadians(signValue * -kDisplacementAngle)];
rotateAnimation.toValue = [NSNumber numberWithFloat:DegreesToRadians(signValue * kDisplacementAngle)];
rotateAnimation.duration = (self.duration);
rotateAnimation.removedOnCompletion = NO;
// leaves presentation layer in final state; preventing snap-back to original state
rotateAnimation.fillMode = kCAFillModeBoth;
rotateAnimation.repeatCount = 0;
rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

// add the animation to the selection layer. This causes it to begin animating.
[metronomeArm.layer addAnimation:rotateAnimation forKey:@"rotateAnimation"];

sleep(2);



robbieduncan
Jul 20, 2009, 03:18 AM
When you call sleep you are sleeping the current thread (probably the main thread). This will stop all drawing/UI events everything. Don't do it. Don't call sleep. Use a NSTimer instead.

PhoneyDeveloper
Jul 20, 2009, 12:04 PM
In addition to what robbie said, the animation code that you show schedules the animation to be executed. The animation isn't executed until sometime later. It normally happens 'soon' but not right away when you execute that code. sleeping the main thread prevents the animation from starting.

If you want something to happen after an animation is finished use setAnimationDidStopSelector. Use a delayed perform or a timer to schedule something at a later time.