I'd like to customize the animation styles when a UICollectionViewCell is inserted and/or deleted.
The reason why I need this is that by default I see that inserting a cell has a smooth fade in animation, however deleting a cell has a combination of move-to-the-left + fade out animation. I would be very happy with this if not for one problem.
After I delete a cell, it is still reused when I add new ones, and when it's reused it's added not with the default fade in effect, but instead it's a combination of move-to-the-left + fade in.
I'm not sure why I'm getting this inconsistency in animations. If this is a known bug/problem/stupidity(on my side :)) please let me know how to fix it.
Otherwise, let me know how to set custom animations when the cell is deleted (or point me towards a tutorial).
Thanks
UPDATE
Fixed the weird animation behavior by subclassing UICollectionViewFlowLayout and adding this line of code
- (UICollectionViewLayoutAttributes *) initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath {
return nil;
}
That's it! :)
Download the circle Layout. It is a sample custom layout that using
That will be a good working material for you.
If you use your own subclass of
UICollectionViewLayout
, you can implement the methods:initialLayoutAttributesForAppearingItemAtIndexPath:
for insertionsfinalLayoutAttributesForDisappearingItemAtIndexPath:
for deletionsAccording to the documentation, the attributes you return are used as starting points for the animation, and the end point are the normal attributes returned by your layout (or the opposite for deletion). Layout attributes include position, alpha, transform... Of course, it is more work to write your own layout class than to use the Apple provided flow layout.
Edit: To answer your question in the comments, here is a super basic implementation of a layout for rows of items which are all the same size.
A cell has a
frame
and, by default, analpha
of 1.0 (as defined bylayoutAttributesForItemAtIndexPath:
). When it is deleted, its properties will be animated from its current state before the deletion to the properties set byfinalLayoutAttributesForDisappearingItemAtIndexPath:
, which correspond to the sameframe
and analpha
of 0.0. So it won't move but it will fade out. However, the cells to the right are going to be moved to the left (because theirindexPath
has changed, and thus theirframe
as set bylayoutAttributesForItemAtIndexPath:
).