0

我有一个 UICollectionCell 的插入动画(如滑入),我在这个方法中调用了插入动画-collectionView:willDisplayCell:forItemAtIndexPath:[self.collectionView reloadData];当我调用它来重新加载数据时,一切正常。这样,动画将在当前显示。

------------
A
------------
<= inserted C (slide-in Animation)
------------
B
------------

但是当我尝试向这个插入的单元格添加另一个动画(如向下滑动)时,出现了问题。-reloadData我没有使用 ,而是使用[self.collectionView performBatchUpdates:completion:]自定义 UICollectionViewFlowLayoutinitialLayoutAttributesForAppearingItemAtIndexPath方法来实现向下滑动动画。

------------
A
------------
------------ <= inserted C (Second, slide-in animation)
B (First, slide-down animation)
------------

在单元格中,动画的委托方法的日志animationDidStop:finished:显示finished = NO

UICollectionCell:
- (void)setAnimationInLayer:(CALayer *)layer
{
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    anim.fromValue = [NSNumber numberWithFloat:(layer.bounds.size.width * 0.33f)];
    anim.toValue  = [NSNumber numberWithFloat:0.0];
    CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnim.fromValue = [NSNumber numberWithFloat:0];
    opacityAnim.toValue = [NSNumber numberWithFloat:1];
    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    animGroup.animations = [NSArray arrayWithObjects:anim,opacityAnim, nil];
    animGroup.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
    animGroup.duration = 5.5f;
    animGroup.delegate = self;
    [layer addAnimation:animGroup forKey:nil];
}
- (void)animationDidStart:(CAAnimation *)animation
{
    NSLog(@"-------> %s",__func__);
    NSLog(@"%f",[self.articlesCollection.layer presentationLayer].frame.origin.x);
}
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)finished
{
    NSLog(@"-------> %s finished: %d",__func__,finished);
    NSLog(@"%f",[self.articlesCollection.layer presentationLayer].frame.origin.x);
}

综上所述,当我使用-performBatchUpdates:completion代替时-reloadData, UICollectionView 的每个动画都消失了。那么问题是什么情况会导致动画消失呢?我的意思是看起来它已经从它所附加的层中删除了。但我没有那样做。任何想法?

4

0 回答 0