Do I need to remove the CABasicAnimation when the animation is over?

I am animating the layer this way to change the background color:

CALayer *layer = [CALayer layer];
            layer.frame = CGRectMake(0, self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height);
            layer.backgroundColor = [UIColor yellowColor].CGColor;
            [self.view.layer insertSublayer:layer atIndex:0];
            [CATransaction begin];
            CABasicAnimation *animation = [CABasicAnimation animation];
            animation.keyPath = @"position.y";
            animation.byValue = @(-self.view.bounds.size.height);
            animation.duration = 0.4;
            animation.fillMode = kCAFillModeForwards;
            animation.removedOnCompletion = NO;
            self.animating=YES;
            [CATransaction setCompletionBlock:^{
                NSLog(@"finished");
                self.view.backgroundColor=[UIColor yellowColor];
                [layer removeFromSuperlayer];
                self.animating=NO;
            }];
            [layer addAnimation:animation forKey:@"slide"];
            [CATransaction commit];

      

When I'm done, I set the actual background color of the UIView and then remove the layer. Do I need to call something like [self.view.layer removeAllAnimations];

in the completion block to remove the animation, or is it removed automatically? Just think about memory management.

+3


source to share


2 answers


No need to speak

animation.fillMode = kCAFillModeForwards;
animation.removedOnCompletion = NO;

      



... if you structure your animation and layer correctly from the start. This whole dance is based on an old misconception about how animation works. Until now, unfortunately, there is often surfing the Internet, but this is wrong and has always been wrong. Better to learn how to do this properly (by setting the layer to its final property value as the animation starts).

+4


source


I don't think you need to delete it at all in this case.

In your completion handler, you remove the layer from your super layer and you don't hold onto the layer anywhere. The layer hierarchy is the only thing that keeps your layer, so when you remove it from the hierarchy, the layer will be deallocated.



Any animations added to the layer must also be released.

0


source







All Articles