UIView animation stops when view disappears, doesn't resume when it re-appears

chrysb picture chrysb · Sep 5, 2013 · Viewed 8.4k times · Source

Here's my config:

I have a storyboard with a ListViewController. ListViewController has a subview UIView called EmptyListView. EmptyListView is shown only when there are no items in the UITableView, otherwise it is hidden.

EmptyListView has a subview UIImageView called emptyListViewArrow which visually points towards the button to create a new entry in my UITableView. This arrow is animated infinitely in an up & down fashion.

I listen for EmptyListView to send a notification when it has finished laying out it's subviews. I do this because if not, animations that mutate constraints behave incorrectly.

- (void) layoutSubviews {
    [super layoutSubviews];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"EmptyViewDidLayoutSubviews" object:nil];
}

When ListViewController observes this notification, it begins the animation:

[UIView animateWithDuration:0.8f delay:0.0f options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse) animations:^{
    self.emptyListViewArrowVerticalSpace.constant = 15.0f;
    [emptyListView layoutIfNeeded];
} completion:nil];

If I put the app in the background or push another ViewController on the stack, once I come back to the app or the ListViewController, the animation is stopped/paused. I can't get it to start again.

I've tried:

  1. Listening for application will resign/become active. When resigning, I did [self.view.layer removeAllAnimations] and then tried to start the animation again using the code above.
  2. Removing the UIImageView being animated and adding it back to the parent view, and then tried to start the animation.

I'm regretting using constraints here, but would love any insight into what could be the problem.

Thank you!

Answer

Vakas picture Vakas · Oct 1, 2018

I was having the same issue. Resolved with the following.

yourCoreAnimation.isRemovedOnCompletion = false

Or you have group of animations

yourGroupAnimation.isRemovedOnCompletion = false