2

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

提前谢谢你,所以。

4

2 回答 2

0

放:

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

前:

[heartLayer addAnimation:theAnimation forKey:@"animateOpacity"];
于 2012-06-05T21:02:59.433 回答
0

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

于 2012-06-05T21:03:38.403 回答