Objective c Fadein/out文本动画

Objective c Fadein/out文本动画,objective-c,ios,animation,Objective C,Ios,Animation,我使用NSTimer(持续5秒)和选择器从NSArray中随机选择名称 这里有一些代码 -(无效)选择播放器:(n定时器*)定时器 { 如果([timer.userInfo timeIntervalSinceNow]

我使用NSTimer(持续5秒)和选择器从NSArray中随机选择名称

这里有一些代码

-(无效)选择播放器:(n定时器*)定时器
{
如果([timer.userInfo timeIntervalSinceNow]<0)
{
[计时器失效];
}
NSString*playerName=[[NSString alloc]initWithFormat:@“%@”,
[self.playersNames objectAtIndex:random()&[self.playersNames count]-1];
self.playersLabel.text=playerName;
[播放名称释放];
}
那个代码工作得很好。self.playersLabel每隔0.2秒填充一个随机名称

我想要的是在PlayerLabel中更改名称时添加一些fadein/fadeout动画效果


如何实现此目的?

您可以使用此选项设置文本:

    [UIView animateWithDuration:0.4 animations:^{
        self.playersLabel.alpha = 0;
    } completion:^(BOOL finished) {
        self.playersLabel.text = @"Other text";
        [UIView animateWithDuration:0.4 animations:^{
            self.playersLabel.alpha = 1;
        }];
    }];

更改
playersLabel

以下是实现此目标的方法:

        [UIView animateWithDuration:0.1 delay:0.f options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         playersLabel.alpha = 0.f;
                     } completion:^(BOOL complete){ 
                         [UIView animateWithDuration:0.1 delay:0.f options:UIViewAnimationOptionCurveEaseInOut
                                          animations:^{  
                                            playersLabel.text = playerName;
                                            playersLabel.alpha = 1.f;
                                          } completion:NULL];
                     }];

我使用第一个动画的完成块重置标签的alpha值。每个动画持续0.1秒,因为您的
playerName
每0.2秒(0.1*2)被选择一次。您可以使用
UIViewAnimationOptionAutoreverse
,但是如果我记得它不会重置标签的alpha属性。

如果字符串长于UILabel字段,是否可以使用相同的动画使NSString在UILabel中进行转换。。???
    [UIView animateWithDuration:0.4 animations:^{
        self.playersLabel.alpha = 0;
    } completion:^(BOOL finished) {
        self.playersLabel.text = @"Other text";
        [UIView animateWithDuration:0.4 animations:^{
            self.playersLabel.alpha = 1;
        }];
    }];
        [UIView animateWithDuration:0.1 delay:0.f options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         playersLabel.alpha = 0.f;
                     } completion:^(BOOL complete){ 
                         [UIView animateWithDuration:0.1 delay:0.f options:UIViewAnimationOptionCurveEaseInOut
                                          animations:^{  
                                            playersLabel.text = playerName;
                                            playersLabel.alpha = 1.f;
                                          } completion:NULL];
                     }];