淡入淡出,淡出动画到uilabel

时间:2012-07-06 07:16:08

标签: objective-c xcode animation 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];
}

从这段代码我得到这种情况:我的标签淡入然后我没有看到淡出动画。 我该如何解决?

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:2)

setAnimationDidStopSelector的调用应该在提交动画之前进行:

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

scoreLabel.alpha = 1;

[UIView commitAnimations];