14

我正在编写一个自定义UICollectionViewFlowLayout,我注意到了这一点initialLayoutAttributesForAppearingItemAtIndexPath:,当我在集合视图上initialLayoutAttributesForAppearingDecorationElementOfKind:atIndexPath:调用时,所有部分都会调用它。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:代替简单的更新,但是无论如何,已经存在的部分(所有部分)都是动画的。我想出了一个检查解决方案,以确保我更改了最后一部分的布局属性,但感觉很hacky。

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

这是只为某些部分设置动画的正确方法吗?

4

1 回答 1

7

好的,我有答案了;它并没有比前一个漂亮很多,但它使布局不会接触数据源,所以它更干净。

基本上,我们需要覆盖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];
}

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

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

这还具有更好的旋转动画的额外好处。

于 2013-01-13T23:12:23.600 回答