目标C语言 UICollectionView的performBatchUpdates:动画所有的部分



我正在编写一个自定义的UICollectionViewFlowLayout,我注意到当我在集合视图上调用performBatchUpdates:completion:时,initialLayoutAttributesForAppearingItemAtIndexPath:initialLayoutAttributesForAppearingDecorationElementOfKind:atIndexPath:将被所有部分调用。结果是所有部分都应用了动画,而不是只是新添加的部分。

[collectionView performBatchUpdates:^{
    currentModelArrayIndex++;
    [collectionView insertSections:[NSIndexSet indexSetWithIndex:currentModelArrayIndex]];
    [collectionView reloadSections:[NSIndexSet indexSetWithIndex:currentModelArrayIndex-1]];
} completion:^(BOOL finished) {
    [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:currentModelArrayIndex] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
}];

到目前为止,我所尝试的是删除对performBatchUpdates:completion:的调用,而不是简单的更新,但已经存在的部分(所有这些)无论如何都是动画的。我想出了一个检查的解决方案,以确保我只在最后一节修改的布局属性,但它感觉很粗糙。

if (decorationIndexPath.section == [(id<UICollectionViewDataSource>)self.collectionView.delegate numberOfSectionsInCollectionView:self.collectionView] - 1)
{
   layoutAttributes.alpha = 0.0f;
   layoutAttributes.transform3D = CATransform3DMakeTranslation(-CGRectGetWidth(layoutAttributes.frame), 0, 0);
}

这是正确的方式去动画只有一些部分吗?

好了,我有答案了;它并不比前一个漂亮多少,但它使布局不触及数据源,因此更干净。

基本上,我们需要覆盖prepareForCollectionViewUpdates:finalizeCollectionViewUpdates来跟踪我们插入的部分。我有一个可变集合,它包含了我们要插入的section的NSNumber实例。

-(void)prepareForCollectionViewUpdates:(NSArray *)updateItems
{
    [super prepareForCollectionViewUpdates:updateItems];
    [updateItems enumerateObjectsUsingBlock:^(UICollectionViewUpdateItem *updateItem, NSUInteger idx, BOOL *stop) {
        if (updateItem.updateAction == UICollectionUpdateActionInsert)
        {
            [insertedSectionSet addObject:@(updateItem.indexPathAfterUpdate.section)];
        }
    }];
}
-(void)finalizeCollectionViewUpdates
{
    [super finalizeCollectionViewUpdates];
    [insertedSectionSet removeAllObjects];
}

接下来,在为项目和装饰视图设置初始布局属性时,检查索引路径的section是否包含在该集合中。

-(UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingDecorationElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)decorationIndexPath
{
    UICollectionViewLayoutAttributes *layoutAttributes;
    if ([elementKind isEqualToString:AFCollectionViewFlowLayoutBackgroundDecoration])
    {
        if ([insertedSectionSet containsObject:@(decorationIndexPath.section)])
        {
            layoutAttributes = [self layoutAttributesForDecorationViewOfKind:elementKind atIndexPath:decorationIndexPath];
            layoutAttributes.alpha = 0.0f;
            layoutAttributes.transform3D = CATransform3DMakeTranslation(-CGRectGetWidth(layoutAttributes.frame), 0, 0);
        }
    }
    return layoutAttributes;
}
-(UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
    UICollectionViewLayoutAttributes *layoutAttributes;
    if ([insertedSectionSet containsObject:@(itemIndexPath.section)])
    {
        layoutAttributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath];
        layoutAttributes.transform3D = CATransform3DMakeTranslation([self collectionViewContentSize].width, 0, 0);
    }
    return layoutAttributes;
}

我从这些方法返回nil,否则,因为nil是默认值。

这还有一个额外的好处,那就是有更好的旋转动画。

最新更新