Has anyone seen a decent answer to this problem?
initialLayoutAttributesForAppearingItemAtIndexPath
seems to be being called for all visible cells, not just
You're not alone. The UICollectionViewLayout header file comments make things a little clearer.
For each element on screen before the invalidation, finalLayoutAttributesForDisappearingXXX will be called and an animation setup from what is on screen to those final attributes.
For each element on screen after the invalidation, initialLayoutAttributesForAppearingXXX will be called an an animation setup from those initial attributes to what ends up on screen.
Basically finalLayoutAttributesForDisappearingItemAtIndexPath
is called for each item on screen before the animation block starts, and initialLayoutAttributesForAppearingItemAtIndexPath
is called for each item after the animation block ends. It's up to you to cache the array of UICollectionViewUpdateItem
objects sent in prepareForCollectionViewUpdates
so you know how to setup the initial and final attributes. In my case I cached the previous layout rectangles in prepareLayout
so I knew the correct initial positions to use.
One thing that stumped me for a while is you should use super's implementation of initialLayoutAttributesForAppearingItemAtIndexPath
and modify the attributes it returns. I was just calling layoutAttributesForItemAtIndexPath
in my implementation, and animations weren't working because the layout positions were different.