gpt4 book ai didi

objective-c - CABasic 完成动画后不删除图像

转载 作者:行者123 更新时间:2023-12-01 16:56:55 25 4
gpt4 key购买 nike

我在 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,但图像仍然卡在 View 上。我想让图像在动画完成后消失。我会很感激我能在这里得到的任何帮助。

提前谢谢你,所以。

最佳答案

设置removedOnCompletionYES确保动画在完成时从图像层中移除,而不是图像层从超层中移除。您需要添加委托(delegate),跟踪完成事件,然后调用 removeFromSuperview在你看来。实现- (void)animationDidStart:(CAAnimation *)theAnimation方法,然后调用 [heartLayer removeFromSuperlayer] .

关于objective-c - CABasic 完成动画后不删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10904876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com