首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >UICollectionView动画(插入/删除项目)

UICollectionView动画(插入/删除项目)
EN

Stack Overflow用户
提问于 2013-05-22 19:41:32
回答 2查看 41K关注 0票数 27

我想在插入和/或删除UICollectionViewCell时自定义动画样式。

之所以需要这样做,是因为在默认情况下,我看到插入单元格具有平滑的淡入动画,而删除单元格则具有向左移动+淡出动画的组合。如果不是因为一个问题,我会非常高兴。

在我删除一个单元格后,当我添加新的单元格时,它仍然会被重用,当它被重用时,它不是默认的淡入效果,而是向左移动+淡入的组合。

我不确定为什么我在动画中会遇到这种不一致的情况。如果这是一个已知的bug/问题/愚蠢(在我这边:)),请让我知道如何修复它。

否则,让我知道如何在删除单元格时设置自定义动画(或指向教程)。

谢谢

更新

通过将UICollectionViewFlowLayout子类化并添加以下代码行,修复了奇怪的动画行为

代码语言:javascript
复制
- (UICollectionViewLayoutAttributes *) initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath {

      return nil;
}

就是这样!:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-22 19:59:21

如果您使用自己的UICollectionViewLayout子类,则可以实现以下方法:

用于删除的insertions

  • finalLayoutAttributesForDisappearingItemAtIndexPath:
  • initialLayoutAttributesForAppearingItemAtIndexPath:

根据文档,返回的属性用作动画的起始点,而终点是布局返回的法线属性(或者相反,用于删除)。布局属性包括位置、alpha、变换...当然,与使用Apple提供的流布局相比,编写自己的布局类要做更多的工作。

编辑:为了在评论中回答你的问题,这里是一个非常基本的布局实现,用于所有相同大小的项目行。

单元格具有frame,默认情况下,alpha为1.0 (由layoutAttributesForItemAtIndexPath:定义)。当它被删除时,它的属性将从删除前的当前状态动画到由finalLayoutAttributesForDisappearingItemAtIndexPath:设置的属性,这些属性对应于相同的framealpha 0.0。所以它不会移动,但会淡出。然而,右边的单元格将被移到左边(因为它们的indexPath已经改变,因此它们的framelayoutAttributesForItemAtIndexPath:设置)。

代码语言:javascript
复制
- (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;
}
票数 33
EN

Stack Overflow用户

发布于 2013-05-22 20:41:58

下载circle Layout。它是一个示例自定义布局,它使用

代码语言:javascript
复制
initialLayoutAttributesForAppearingItemAtIndexPath:  
finalLayoutAttributesForDisappearingItemAtIndexPath:  

这对你来说将是一个很好的工作材料。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16690831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档