2012-06-05 30 views
2

我在播放声音的NIB文件中有一个按钮,并在下面调用此方法。完成动画后CABasic不会删除图像

-(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。我想在动画完成后让图像消失。我很感激任何帮助,我可以在这里。

谢谢你,SO。

回答

0

设置removedOnCompletionYES确保动画从上完成图像的层去除,而不是图像层从superlayer删除。您需要添加委托,跟踪完成事件,并在您的视图上致电removeFromSuperview。在代表上实施- (void)animationDidStart:(CAAnimation *)theAnimation方法,然后致电[heartLayer removeFromSuperlayer]

+0

我忘了添加一个委托。非常感谢你的帮助,善良的先生! –

+0

@RehatKathuria不客气!如果答案适合您,请考虑通过点击答案旁边复选标记的大纲来接受答案。这会告诉其他人,你不再寻找更好的答案,并在Stack Overflow上为你赢得全新的徽章。 – dasblinkenlight

0

地说:

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

前:

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