objective-c - UICollectionView performBatchUpdates : animates all sections

标签 objective-c ios6 uikit uicollectionview uicollectionviewlayout

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

[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 来跟踪我们插入的部分。我有一个可变集,其中包含我们要插入的部分的 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];
}

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

-(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 是默认值。

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

关于objective-c - UICollectionView performBatchUpdates : animates all sections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14307494/

相关文章:

ios - 当 SDK 在 Objective-C 中时,Swift 扩展在测试中不起作用

iphone - 在 Objective-C 中,欧拉数?

iphone - 我的应用程序的更新是否应该与iPhone 5屏幕兼容?

iphone - 使用 imageEdgeInsets 和 titleEdgeInsets 对齐 UIButton 上的文本和图像

ios - 自定义 UITableViewCells 和 UITableView 的问题

objective-c - 如何桥接一个以闭包为参数的函数到 objc

xcode - 在带有 Cordova 2.1 的 xcode 4.5 中启动图像不会更新

ios - iOS6 上的推特库

ios - 动画 View 约束而不动画其他 View

ios - 如果数据源为空,tableview 的占位符文本