I have a button in a NIB file that plays a sound and calls this method below.
-(void) animateHeart
{
heartLayer = [[CALayer alloc] init];
[heartLayer setBounds:CGRectMake(0.0, 0.0, 85.0, 85.0)];
[heartLayer setPosition:CGPointMake(150.0, 100.0)];
UIImage *heartImage = [UIImage imageNamed:@"heart.png"];
CGFloat nativeWidth = CGImageGetWidth(heartImage.CGImage) / 3;
CGFloat nativeHeight = CGImageGetHeight(heartImage.CGImage) / 3;
CGRect startFrame = CGRectMake(165.0, 145.0, nativeWidth, nativeHeight);
heartLayer.contents = (id)heartImage.CGImage;
heartLayer.frame = startFrame;
[self.view.layer addSublayer:heartLayer];
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
theAnimation.duration=2.5;
theAnimation.repeatCount=2;
theAnimation.speed = 1.85;
theAnimation.autoreverses=YES;
theAnimation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
theAnimation.fromValue=[NSNumber numberWithFloat:0.0];
theAnimation.toValue=[NSNumber numberWithFloat:1.0];
[theAnimation setValue:heartLayer forKey:@"parentLayer"];
[heartLayer addAnimation:theAnimation forKey:@"animateOpacity"];
theAnimation.fillMode = kCAFillModeRemoved;
theAnimation.removedOnCompletion = YES;
}
The method animates the image perfectly fine but after having done so, the image is left hanging there on the view despite having set the removedOnCompletion BOOL to true. I would like to have the image disappear once the animation has completed. I’d appreciate any help I could get here.
Thank you in advance, SO.
Setting
removedOnCompletiontoYESensures that the animation is removed from the image’s layer on completion, not that the image layer is removed from the superlayer. You need to add a delegate, track the completion event, and callremoveFromSuperviewon your view. Implement- (void)animationDidStart:(CAAnimation *)theAnimationmethod on the delegate, then call[heartLayer removeFromSuperlayer].