使用invalidateLayout调用的高级UICollectionView动画

Mic*_*est 12 core-animation ios ios6 uicollectionview

UICollectionView编程指南说:

除了动画插入,删除和移动操作之外,您还可以随时使布局无效并强制它重绘其内容.使布局无效不会直接为项目设置动画; 当您使布局无效时,集合视图会在没有动画的情况下显示新计算位置中的项目.但是,使布局无效的行为会导致布局对象显式移动项目.在自定义布局中,您可以使用此行为以固定间隔定位单元格并创建动画效果.

我一直在尝试这样的东西AnimationStep,我的UICollectionViewFlowLayout子类使用枚举来有条件地设置具有三阶段动画的tile的位置:

-(void)update{
    [self animateLayoutAfterDelay:2.0 toStep:AnimationStepOne];
    [self animateLayoutAfterDelay:4.0 toStep:AnimationStepTwo];
    [self animateLayoutAfterDelay:6.0 toStep:AnimationStepThree];
}

-(void)animateLayoutAfterDelay:(NSTimeInterval)delay toStep:(MagazineLayoutAnimationStep)step {
    double delayInSeconds = delay;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [UIView animateWithDuration:2.0 animations:^{
            self.layout.animationStep = step;
            [self.layout invalidateLayout];
        }];
    });
}
Run Code Online (Sandbox Code Playgroud)

这具有非常不可预测的影响.有些单元格以我期望的方式生成动画,有些单元格被隐藏并显示或只是出现在新位置.我在细胞上放置了随机的背景颜色,看看这是否可能是UICollectionView回收细胞的影响,当然,有时也是如此.这解释了一些奇怪的东西,但不是全部.

有人知道Apple是如何让我为细胞设置动画并移动它们吗?

(我需要这个,因为我的集合视图单元格往往会改变大小,我想要一个没有任何对角线移动的优雅动画).

Mic*_*est 8

答案是使用中间布局.

[self.collectionView setCollectionViewLayout: layoutForStepOne animated:YES completion:^(BOOL finished) {
    [self.collectionView setCollectionViewLayout: layoutForStepTwo animated:YES completion:^(BOOL finished) {
        [self.collectionView setCollectionViewLayout: layoutForStepThree animated:YES];

    }];
}];
Run Code Online (Sandbox Code Playgroud)