由于系统原因不明,UIView动画被系统取消了

时间:2018-02-06 09:16:13

标签: ios objective-c animation uicollectionview performbatchupdates

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

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

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

------------
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的每个动画都消失了。 所以问题是什么情况可能导致动画消失? 我的意思是看起来它已经从它所连接的层中移除了。但我没有这样做。有什么想法吗?

0 个答案:

没有答案