UILabel的淡入淡出动画

6
我有一个标签需要淡入淡出。以下是我的代码:
```html

我有一个标签需要淡入淡出。

```
-(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个回答

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];
}

谢谢!我忘记使用setAnimationDelegate了,现在它完美地工作了! - user1492776

2
调用 setAnimationDidStopSelector 应该在提交动画之前:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:2];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

scoreLabel.alpha = 1;

[UIView commitAnimations];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接