ios – 使用自定义布局的UICollectionView接口旋转

我在旋转设备时看到了UICollectionView的一些神秘行为,它带有自定义布局(UICollectionViewLayout的子类).

我有一个简单的水平滚动的单元格行.当设备从纵向旋转到横向时,之前看不到的其他单元格变得可见,并且那些出现的单元格的动画是错误的(它使用熟悉的重影效果,我认为这是集合视图的某种默认动画效果布局).请注意此动画中最左侧出现的单元格:

有关自定义布局设置的一些细节:

> shouldInvalidateLayoutForBoundsChange:返回YES.
>在layoutAttributesForItemAtIndexPath中:如果尚未创建属性,则在字典中缓存这些属性.
>在layoutAttributesForElementsInRect中:我计算手工可见的单元格,并在返回属性之前每次稍微调整它们的中心属性.

然后我有以下代码来处理初始/最终布局属性:

- (void)prepareForAnimatedBoundsChange:(CGRect)oldBounds
{
    [super prepareForAnimatedBoundsChange:oldBounds];
    self.animatingBoundsChange = YES;
}

- (void)finalizeAnimatedBoundsChange
{
    [super finalizeAnimatedBoundsChange];
    self.animatingBoundsChange = NO;
}

- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
    if (self.animatingBoundsChange) {
        // If the view is rotating,appearing items should animate from their current attributes (specify `nil`).
        // Both of these appear to do much the same thing:
        //return [self layoutAttributesForItemAtIndexPath:itemIndexPath];
        return nil;
    }
    return [super initialLayoutAttributesForAppearingItemAtIndexPath:itemIndexPath];
}

- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath
{
    if (self.animatingBoundsChange) {
        // If the view is rotating,disappearing items should animate to their new attributes.
        return [self layoutAttributesForItemAtIndexPath:itemIndexPath];
    }
    return [super finalLayoutAttributesForDisappearingItemAtIndexPath:itemIndexPath];
}

在我看来,新出现的单元格的初始布局属性在某种程度上是不正确的(它毕竟在正确的位置结束).但是当我记录从initalLayout …方法返回的布局属性的center属性时,一切看起来都是正确的 – 所有都沿着水平轴均匀分布.

没有正确动画的单元格的唯一区别特征是,当调用initialLayout …方法时,它的单元格不会在[collectionView visibleCells]数组中返回. layoutAttributesForElementsInRect:在它之前确实正确地标识了它的布局属性.

发生什么了?对引擎盖下发生的事情的一些见解会非常有用…… UICollectionView看起来像一个巨大的黑盒子.

解决方法

the answer that Arun_k linked to的基础上,似乎你可能需要返回与initialLayoutAttributesForAppearingItemAtIndexPath不同的东西:除了nil之外的新单元格正在出现. nil适用于已经在屏幕上的单元格,但可能不适合返回新单元格.也许您可以为此方法中的所有单元格返回固定的UICollectionViewLayoutAttributes值,以查看它是否更改了新插入的单元格的动画行为,然后从那里继续.

这个问题在这个问题上有了更多的启示:initialLayoutAttributesForAppearingItemAtIndexPath fired for all visible cells,not just inserted cells

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...