gpt4 book ai didi

ios - 使用 invalidateLayout 调用的高级 UICollectionView 动画

转载 作者:技术小花猫 更新时间:2023-10-29 10:37:34 26 4
gpt4 key购买 nike

UICollectionView Programming Guide说:

In addition to animating insertions, deletions, and move operations, you can invalidate the layout at any time and force it to redraw its contents. Invalidating the layout does not animate items directly; when you invalidate the layout, the collection view displays the items in their newly calculated positions without animating. However, the act of invalidating the layout causes the layout object to move items explicitly. In a custom layout, you might use this behavior to position cells at regular intervals and create an animated effect.

我一直在尝试这样的事情,其中​​ AnimationStep 是我的 UICollectionViewFlowLayout 子类使用的枚举,用于有条件地设置具有三阶段动画的图 block 位置:

-(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];
}];
});
}

这具有相当不可预知的效果。有些单元格以我期望的方式进行动画处理,有些单元格会隐藏和显示,或者只是出现在它们的新位置。我在单元格上放置了随机背景颜色,看看这是否可能是 UICollectionView 回收单元格的效果,果然,有时是这样。这解释了一些奇怪的地方,但不是全部。

有人知道 Apple 希望我如何为单元格设置动画并移动它们吗?

(我需要这个,因为我的 Collection View 单元格往往会改变大小,我想要一个没有任何对角线移动的优美动画)。

最佳答案

答案是使用中间布局。

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

}];
}];

关于ios - 使用 invalidateLayout 调用的高级 UICollectionView 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15566735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com