Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kimicnoo

macrumors newbie
Original poster
Jul 19, 2009
1
0
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..
Code:
    // 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);
 
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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.