Using CABasicAnimation with sleep()

Discussion in 'iOS Programming' started by kimicnoo, Jul 19, 2009.

  1. kimicnoo macrumors newbie

    Jul 19, 2009
    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"];
  2. robbieduncan Moderator emeritus


    Jul 24, 2002
    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.
  3. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    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.

Share This Page