[Question] Animating UICollectionView cell frames smoothly

Question

I'm trying to animate between 2 states of my UICollectionView layout and I'm having trouble getting it to happen smoothly. The cell frames animate smoothly but the contents are snapping between cell sizes.

I've setup a repository and an example which demonstrates the issue as simply as I can: https://github.com/skorulis/LayoutTest

Anyone have any tips on how to solve this? It seems like it's something that should be simple but so far I haven't had any luck in finding a solution.

Posted Jan 28, 2016, 8:20 AM UTC

24
criosist•

OK i checked the way i am changing layouts on my testing thing i have and i dont put it in the CV perform Batch updates, I just invalidate layout and call reloadData on the CV ?

1
askoruliOP•

I just tried that and it results in no animation at all.

1
david_phillip_oster•

The snapping you are seeing is because you aren't using UICollectionViewTransitionLayout to drive your internal cell animations in synchrony with the layout animation change.

1
askoruliOP•

I switched the layout invalidation to:

UICollectionViewFlowLayout *layoutNew = [[UICollectionViewFlowLayout alloc] init];
self.view.window.userInteractionEnabled = NO; //Interfering with the animation would cause a crash
[_collectionView startInteractiveTransitionToCollectionViewLayout:layoutNew completion:^(BOOL completed, BOOL finished) {
    _layout = layoutNew;
    self.view.window.userInteractionEnabled = YES;
}];
[_collectionView finishInteractiveTransition];

And now I'm getting the correct animation. It feels a little bit hacky because it's not really an interactive animation but it's doing exactly what I want so I can live with that. Thanks for the tip

2