when my app comes out of the background the animation has stopped. which is normal. but i want to restart my animation from the current state. how do i do that without my snapping all over the place.
[UIView animateWithDuration:60 delay:0 options:(UIViewAnimationOptionCurveLinear |UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState) animations:^{
[bg setFrame:CGRectMake(0, 0, 1378, 1005)];
} completion:nil];
i tried putting a set frame in front of the animation but that just makes it snap.
[bg setFrame:CGRectMake(0, 0, 1378, 1005)];
any ideas?
well the answer of @dany_23 could work.
But I came across an other method that works just fine if you don't need to resume your animation but restart your animation, without the view or layer snapping when you reactivate the app.
in the
you call a method in your viewcontroller which implements the following code.
and in the
you call a method in your viewcontroller that just starts the animation. something like
Hope this helps, Thanks to all who replied.
You can add an observer in your class for UIApplicationWillEnterForegroundNotification:
It is important to set the begin state of the animation (and don't forget to remove the notification observer)
You'll have to pause the animation when your app goes into the background and resume it when it becomes active again. You can find some sample code here that describes how to pause and resume an animation.
I don't recall the exact details but I think you can get the current animated position of the frame by looking at the layer bounds and position property of your view. You could store those at app suspend and restore them when the app is in the foreground again.
This way will be better, just register on ApplicationDelegate will become methods and observer the status.
There is a better soloution here than restarting whole animation each time you come from background.
For Swift 3 you can subclass this class:
It will take care of pausing all your animations in current state and re-adding it when app comes from background - without resetting them.
Gist: https://gist.github.com/grzegorzkrukowski/a5ed8b38bec548f9620bb95665c06128