Search code examples
cocoa-touchcore-animationcabasicanimationviewwillappear

How to get my animation running on viewWillAppear or viewDidAppear


How do I get my animation to run again when viewWillAppear or viewDidAppear get called?

This is my animation:

bounceAnimation =[CABasicAnimation animationWithKeyPath:@"transform.scale"];
[bounceAnimation setToValue:[NSNumber numberWithFloat:1.4f]];
bounceAnimation.duration = 1;
bounceAnimation.repeatCount = 100000;
bounceAnimation.autoreverses = YES;
bounceAnimation.fillMode =kCAMediaTimingFunctionEaseInEaseOut;
bounceAnimation.removedOnCompletion = YES;
[startBtn.layer addAnimation:bounceAnimation forKey:@"bounceAnimation"];

Solution

  • How are you invoking your animation?

    For the moment, I'm thinking you could either

    1) put that above code into a method and call that method from viewDidAppear again.

    2) or, maybe better... set the removedOnCompletion property for your animation to NO via

    bounceAnimation.removedOnCompletion = NO;
    

    and when you are ready to run it again, invoke that same (retained) animation you added previously.