Search code examples
iosuiviewcore-animation

Restart UIView repeat animation


I've set up a a repeating animation in iOS:

[UIView animateKeyframesWithDuration:duration 
                               delay:0.0 
                             options:UIViewKeyframeAnimationOptionRepeat 
                          animations:^{
    ...
}];

As it turns out though, when I present a view controller and later dismiss it - the animation seems to have frozen.

I've researched this and found that animations are stopped when the app goes to the background. But here it's a view controller presented on top. Could this be the same case here?

I've found the solution is to stop the animation before the app exits to the background, as shown on this article by Apple: https://developer.apple.com/library/archive/qa/qa1673/_index.html

I've tried that solution but that hasn't worked for me. Once the view controller is shown, the animation's completion block is called.

How can I resolve this animation freeze situation?


Solution

  • To restart animation after present another view, put it in viewWillAppear

    If your animation start after a while or with condition, simply add a Bool like animationHasStarted, check for it in viewWillAppear to see if you should run the animation or not