【问题标题】:CABasic doesn't rid of image after completing the animationCABasic 完成动画后不删除图像
【发布时间】:2012-06-05 20:59:51
【问题描述】:

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

提前谢谢你,所以。

【问题讨论】:

    标签: objective-c ios ios5 cabasicanimation


    【解决方案1】:

    将removedOnCompletion 设置为YES 可确保动画在完成时从图像层中移除,而不是图像层从超层中移除。您需要添加一个委托,跟踪完成事件,并在您的视图上调用removeFromSuperview。在委托上实现- (void)animationDidStart:(CAAnimation *)theAnimation 方法,然后调用[heartLayer removeFromSuperlayer]。

    【讨论】:

    • 我忘记添加代表了。非常感谢您的帮助,好心的先生!
    • @RehatKathuria 欢迎您!如果答案对您有用,请考虑通过单击答案旁边的复选标记的轮廓来接受它。这将告诉其他人您正在寻找更好的答案,并在 Stack Overflow 上为您赢得一个全新的徽章。
    【解决方案2】:

    放:

    theAnimation.fillMode = kCAFillModeRemoved;
    theAnimation.removedOnCompletion = YES;
    

    之前:

    [heartLayer addAnimation:theAnimation forKey:@"animateOpacity"];
    

    【讨论】:

      猜你喜欢
      • 2020-05-11
      • 2013-07-15
      • 2016-03-09
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多