uicollectionviewuicollectionviewlayoutsectionsflowlayout

UICollectionView - customize item disappearing when section collapses


I collapse & expand sections by toggling numberOfItemsInSection:. When collapsing, items disappear through alpha fade-out. I would like to change this effect to scaling them down.

I think I can implement this with custom UICollectionViewFlowLayout and overloaded layoutAttributesForItemAtIndexPath:. But when section collapses, numberOfItemsInSection: returns 0 - so I can't set destination scale for that items.

What is correct approach?


Solution

  • Have found approach: we need to subclass UICollectionViewFlowLayout and override finalLayoutAttributesForDisappearingItemAtIndexPath:

    For example:

    - (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
    {
        UICollectionViewLayoutAttributes *attributes = [super finalLayoutAttributesForDisappearingItemAtIndexPath:itemIndexPath];
        attributes.frame = CGRectInset(attributes.frame, 20, 20);
        return attributes;
    }