2012-06-05 50 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"];