2012-07-06 121 views
6

我有一个标签,我想淡入然后淡出。 这里是我的代码:淡入,淡出动画到uilabel

-(void) fadein 
{ 
    scoreLabel.alpha = 0; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    [UIView setAnimationDuration:2]; 
    scoreLabel.alpha = 1; 
    [UIView commitAnimations]; 
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
} 



-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:2]; 
scoreLabel.alpha = 0; 
[UIView commitAnimations]; 
} 

从这个代码我得到这样的情况:我的标签是淡入,然后我看不出淡出动画。 我该如何解决它?

回答

11
-(void) fadein 
{ 
    scoreLabel.alpha = 0; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 

    //don't forget to add delegate..... 
    [UIView setAnimationDelegate:self]; 

    [UIView setAnimationDuration:2]; 
    scoreLabel.alpha = 1; 

    //also call this before commit animations...... 
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 
    [UIView commitAnimations]; 
} 



-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 
{ 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:2]; 
    scoreLabel.alpha = 0; 
    [UIView commitAnimations]; 
} 
+0

谢谢!我忘了使用setAnimationDelagate,现在它完美地工作! – user1492776 2012-07-06 07:32:06

+0

欢迎..如果它工作,然后upvote并接受答案:) – mayuur 2012-07-06 07:36:13

2

setAnimationDidStopSelector呼叫前应提交动画:

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
[UIView setAnimationDuration:2]; 
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)]; 

scoreLabel.alpha = 1; 

[UIView commitAnimations];