我正在编写自定义 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/