UICollectionView animations (insert/delete items)

后端 未结 2 751
情歌与酒
情歌与酒 2020-12-01 00:53

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 inserti

相关标签:
2条回答
  • 2020-12-01 01:29

    Download the circle Layout. It is a sample custom layout that using

    initialLayoutAttributesForAppearingItemAtIndexPath:  
    finalLayoutAttributesForDisappearingItemAtIndexPath:  
    

    That will be a good working material for you.

    0 讨论(0)
  • 2020-12-01 01:43

    If you use your own subclass of UICollectionViewLayout, you can implement the methods:

    • initialLayoutAttributesForAppearingItemAtIndexPath: for insertions

    • finalLayoutAttributesForDisappearingItemAtIndexPath: for deletions

    According 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, an alpha of 1.0 (as defined by layoutAttributesForItemAtIndexPath:). When it is deleted, its properties will be animated from its current state before the deletion to the properties set by finalLayoutAttributesForDisappearingItemAtIndexPath:, which correspond to the same frame and an alpha 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 their indexPath has changed, and thus their frame as set by layoutAttributesForItemAtIndexPath:).

    - (CGSize)collectionViewContentSize
    {
        NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:0];
        return CGSizeMake(numberOfItems * ITEM_WIDTH, ITEM_HEIGHT);
    }
    
    - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        NSUInteger index = [indexPath indexAtPosition:0];
        UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        attributes.frame = CGRectMake(index * ITEM_WIDTH, 0, ITEM_WIDTH, ITEM_HEIGHT);
        return attributes;
    }
    
    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
    {
        NSMutableArray *attributes = [NSMutableArray new];
        NSUInteger firstIndex = floorf(CGRectGetMinX(rect) / ITEM_WIDTH);
        NSUInteger lastIndex = ceilf(CGRectGetMaxX(rect) / ITEM_WIDTH);
        for (NSUInteger index = firstIndex; index <= lastIndex; index++) {
            NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndexes:(NSUInteger [2]){ 0, index } length:2];
            [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
        }
        return attributes;
    }
    
    - (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)indexPath
    {
        UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
        attributes.alpha = 0.0;
        return attributes;
    }
    
    0 讨论(0)
提交回复
热议问题