After some weeks struggling with this bug, some helpful insights from our friends @david-h and @erwin and some calls and e-mails from Apple's WWDR, I was able to figure this issue out. Just wanna share the solution with the community.
The problem. UIKit Dynamics has some helper methods to work with Collection Views and these methods don't actually do some internal stuff that I think it should do automatically, like automatically update the dynamically animated cells when some action using performBatchUpdates: is performed. So you have to do it manually according to WWDR, so we iterate over the updated itens updating their NSIndexPaths so the the dynamic animator can give us proper updates on the cells.
The bug. Even doing this indexPath update on a cell insertion/deletion, I had lots of random crashes and strange behaviours with the animation. So, I followed a tip given by @erwin that consists on re-instantiate the UIDynamicAnimator after a performBatchUpdates: and that fixes up all the problems with this kind of situation.
So the code.
- (void)removeItemAtIndexPath:(NSIndexPath *)itemToRemove completion:(void (^)(void))completion
{
UICollectionViewLayoutAttributes *attributes = [self.dynamicAnimator layoutAttributesForCellAtIndexPath:itemToRemove];
if (attributes) {
[self.collisionBehaviour removeItem:attributes];
[self.gravityBehaviour removeItem:attributes];
[self.itemBehaviour removeItem:attributes];
// Fix the problem explained on 1.
// Update all the indexPaths of the remaining cells
NSArray *remainingAttributes = self.collisionBehaviour.items;
for (UICollectionViewLayoutAttributes *attributes in remainingAttributes) {
if (attributes.indexPath.row > itemToRemove.row)
attributes.indexPath = [NSIndexPath indexPathForRow:(attributes.indexPath.row - 1) inSection:attributes.indexPath.section];
}
[self.collectionView performBatchUpdates:^{
completion();
[self.collectionView deleteItemsAtIndexPaths:@[itemToRemove]];
} completion:nil];
// Fix the bug explained on 2.
// Re-instantiate the Dynamic Animator
self.dynamicAnimator = [[UIDynamicAnimator alloc] initWithCollectionViewLayout:self];
[self.dynamicAnimator addBehavior:self.collisionBehaviour];
[self.dynamicAnimator addBehavior:self.gravityBehaviour];
[self.dynamicAnimator addBehavior:self.itemBehaviour];
}
}
I opened a Radar explaining the issue expecting Apple to fix this on a future update. If anyone whats to duplicate it, It's available on OpenRadar.
Thank you everyone.