UIView animation was canceled by a system for an unclear reason

79 Views Asked by At

I have an insert animation for UICollectionCell (like slide-in), I called the insert animation in this method -collectionView:willDisplayCell:forItemAtIndexPath:. Everything just works fine when I called this [self.collectionView reloadData]; to reload data. In this way, the animation will be shown currently.

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

But when I tried to add another animation to this inserted cell (like slide-down), things went wrong. Instead of using -reloadData, I used [self.collectionView performBatchUpdates:completion:] and a custom UICollectionViewFlowLayout with initialLayoutAttributesForAppearingItemAtIndexPath method to implement the slide down animation.

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

In the cell, the log of animation's delegate method animationDidStop:finished: shows that the 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);
}

To sum up, when I use -performBatchUpdates:completion instead of -reloadData, every animation of UICollectionView disappeared. So the question is what situation may cause an animation disappear? I mean looks like it has been removed from the layer it is attached to. But I didn't do that. Any idea?

0

There are 0 best solutions below