我想在插入和/或删除UICollectionViewCell时自定义动画样式。
之所以需要这样做,是因为在默认情况下,我看到插入单元格具有平滑的淡入动画,而删除单元格则具有向左移动+淡出动画的组合。如果不是因为一个问题,我会非常高兴。
在我删除一个单元格后,当我添加新的单元格时,它仍然会被重用,当它被重用时,它不是默认的淡入效果,而是向左移动+淡入的组合。
我不确定为什么我在动画中会遇到这种不一致的情况。如果这是一个已知的bug/问题/愚蠢(在我这边:)),请让我知道如何修复它。
否则,让我知道如何在删除单元格时设置自定义动画(或指向教程)。
谢谢
更新
通过将UICollectionViewFlowLayout子类化并添加以下代码行,修复了奇怪的动画行为
- (UICollectionViewLayoutAttributes *) initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath {
return nil;
}
就是这样!:)
发布于 2013-05-22 19:59:21
如果您使用自己的UICollectionViewLayout
子类,则可以实现以下方法:
用于删除的insertions
finalLayoutAttributesForDisappearingItemAtIndexPath:
的initialLayoutAttributesForAppearingItemAtIndexPath:
根据文档,返回的属性用作动画的起始点,而终点是布局返回的法线属性(或者相反,用于删除)。布局属性包括位置、alpha、变换...当然,与使用Apple提供的流布局相比,编写自己的布局类要做更多的工作。
编辑:为了在评论中回答你的问题,这里是一个非常基本的布局实现,用于所有相同大小的项目行。
单元格具有frame
,默认情况下,alpha
为1.0 (由layoutAttributesForItemAtIndexPath:
定义)。当它被删除时,它的属性将从删除前的当前状态动画到由finalLayoutAttributesForDisappearingItemAtIndexPath:
设置的属性,这些属性对应于相同的frame
和alpha
0.0。所以它不会移动,但会淡出。然而,右边的单元格将被移到左边(因为它们的indexPath
已经改变,因此它们的frame
由layoutAttributesForItemAtIndexPath:
设置)。
- (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;
}
发布于 2013-05-22 20:41:58
下载circle Layout。它是一个示例自定义布局,它使用
initialLayoutAttributesForAppearingItemAtIndexPath:
finalLayoutAttributesForDisappearingItemAtIndexPath:
这对你来说将是一个很好的工作材料。
https://stackoverflow.com/questions/16690831
复制相似问题