benkdev
benkdev

Reputation: 673

Animating Collection View Cells in iOS

When my view controller's view appears, I would like all of the cells in its collection view to flip horizontally.

How can this be done? I looked into UIDynamics and didn't find a flip animation. I've got an animation using Core Animation that will flip a view and I don't know how to apply it to the cells.

Upvotes: 1

Views: 1001

Answers (1)

Hector Matos
Hector Matos

Reputation: 1056

You can use this method in a subclass of UICollectionViewFlowLayout:

- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
    UICollectionViewLayoutAttributes *layoutAttributes = [super initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];

    CATransform3D transform = CATransform3DMakeRotation(DEGREES_TO_RADIANS(180.0), 0.0, 1.0, 0.0);
layoutAttributes.transform3D = transform;

    return layoutAttributes;
}

What this basically does is grabs the initial layout attributes of your collectionViewCell and has it flipped horizontally. On insertion of cells, flowLayout will do a linear animation from these initial attributes to the original attributes of that cell. This method will get called if you call

- (void)performBatchUpdates:(void (^)(void))updates completion:(void (^)(BOOL finished))completion;

and pass in your insertion using

- (void)insertItemsAtIndexPaths:(NSArray *)indexPaths;

into the update block.

Upvotes: 2

Related Questions