我在 NIB 文件中有一个按钮,可以播放声音并在下面调用此方法。
-(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;
}
该方法可以完美地为图像设置动画,但是在这样做之后,尽管已将 removedOnCompletion BOOL 设置为 true,但图像仍然挂在视图上。我想让图像在动画完成后消失。我会很感激我能在这里得到的任何帮助。
提前谢谢你,所以。