2013-05-18 84 views
0

我想imageview淡入淡出,然後完全隱藏。UImagview褪色,然後隱藏

這裏是我的代碼

CABasicAnimation *theAnimation; 
    theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"]; 
    theAnimation.duration=1.0; 
    theAnimation.fromValue=[NSNumber numberWithFloat:1.0]; 
    theAnimation.toValue=[NSNumber numberWithFloat:0.0]; 
    [flowerImageView.layer addAnimation:theAnimation forKey:@"animateOpacity"]; 

這是正常工作,但是當值變爲0.0 ImageView的應該不會再次出現

+0

它淡出之後。你只需從超級視圖中刪除它(這是UIImageView)。 [imageView removeFromSuperview]。 – jamil

+0

但我怎麼會知道這個值變成了0 – user2185354

+0

看看這裏http://stackoverflow.com/a/11487269/1328096。 – jamil

回答

2

其實,有這個所以設置NSTimer

沒有回調方法
 CABasicAnimation *theAnimation; 
    theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"]; 
    theAnimation.duration=1.0; 
    theAnimation.fromValue=[NSNumber numberWithFloat:1.0]; 
    theAnimation.toValue=[NSNumber numberWithFloat:0.0]; 
    [flowerImageView.layer addAnimation:theAnimation forKey:@"animateOpacity"]; 

    [NSTimer scheduledTimerWithTimeInterval:theAnimation.duration 
     target:self 
     selector:@selector(targetMethod) 
     userInfo:nil 
     repeats:NO]; 

動畫完成後調用以下方法

-(void)targetMethod 
{ 
    flowerImageView.hidden = YES; 
} 
1

是的,Pratik是正確的方式,但圖像將在那裏只是它不會顯示給你。

你也可以嘗試下面的解決方案,它會完全隱藏圖像。

CABasicAnimation *theAnimation; 
theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"]; 
theAnimation.duration=1.0; 
theAnimation.fromValue=[NSNumber numberWithFloat:1.0]; 
theAnimation.toValue=[NSNumber numberWithFloat:0.0]; 
[flowerImageView.layer addAnimation:theAnimation forKey:@"animateOpacity"]; 

[NSTimer scheduledTimerWithTimeInterval:theAnimation.duration 
    target:self 
    selector:@selector(targetMethod) 
    userInfo:nil 
    repeats:NO]; 

然後在動畫完成後完全隱藏圖像。

-(void)targetMethod 
{ 
    [flowerImageView setHidden:YES]; 
}