用多个句子动画UILabel

时间:2013-12-21 11:22:01

标签: ios objective-c

我想让uilabel动画每1秒更改一次文字 但是当我运行应用程序时,它会直接显示最后一个单词。即我有4个不同的句子一个接一个地显示,但它直接显示最后一个。


这是我正在使用的代码

[UIView animateWithDuration:1.0
                     animations:^{
                             self.mapStat.alpha = 0.0f;
                             self.mapStat.text = @"Retriving Coordinates";;
                             self.mapStat.alpha = 1.0f;
                     }];
    [UIView animateWithDuration:1.0
                     animations:^{
                         self.mapStat.alpha = 0.0f;
                         self.mapStat.text = @"Retrieved Coordinates";
                         self.mapStat.alpha = 1.0f;
                     }];
    [UIView animateWithDuration:1.0
                     animations:^{
                         self.mapStat.alpha = 0.0f;
                         self.mapStat.text = @"Applying Coordinates";
                         self.mapStat.alpha = 1.0f;
                     }];
    [UIView animateWithDuration:1.0
                     animations:^{
                         self.mapStat.alpha = 0.0f;
                         self.mapStat.text = @"Loading Map!";
                         self.mapStat.alpha = 1.0f;
                     }];

代码有什么问题?
有没有更简单的方法来执行这个动画?

3 个答案:

答案 0 :(得分:2)

您的代码没有任何问题。错误的是,您不是阅读文档,而是假设text的{​​{1}}属性可以设置动画。它不能。


如果要淡入淡出文本,可以使用UILabel的{​​{1}}方法,并在标签淡出后更改完成块中的文本。

答案 1 :(得分:1)

问题是你没有为你的代码设置下一个动画的延迟,你设置完整的先前动画的延迟时间,比如这个

[UIView animateWithDuration:1.0f
                          delay:0.0f
                        options:UIViewAnimationOptionAutoreverse
                     animations:^
     {
         self.mapStat.alpha = 0.0f;
         self.mapStat.text = @"Retriving Coordinates";
         self.mapStat.alpha = 1.0f;
     }
    completion:^(BOOL finished)
     {
    }];
    [UIView animateWithDuration:1.0f
                          delay:1.0f
                        options:UIViewAnimationOptionAutoreverse
                     animations:^
     {
         self.mapStat.alpha = 0.0f;
         self.mapStat.text = @"Retriving Coordinates";
         self.mapStat.alpha = 1.0f;
     }
                     completion:^(BOOL finished)
     {
     }];
    [UIView animateWithDuration:1.0f
                          delay:2.0f
                        options:UIViewAnimationOptionAutoreverse
                     animations:^
     {
         self.mapStat.alpha = 0.0f;
         self.mapStat.text = @"Applying Coordinates";
         self.mapStat.alpha = 1.0f;
     }
                     completion:^(BOOL finished)
     {
     }];
    [UIView animateWithDuration:1.0f
                          delay:2.0f
                        options:UIViewAnimationOptionAutoreverse
                     animations:^
     {
         self.mapStat.alpha = 0.0f;
         self.mapStat.text = @"Loading Map!";
         self.mapStat.alpha = 1.0f;
     }
                     completion:^(BOOL finished)
     {
     }];

答案 2 :(得分:0)

试试这个

int NoOfStringsLeft =  4;

[NSTimer scheduledTimerWithTimeInterval:1.0
        target:self
        selector:@selector(targetMethod:)
        userInfo:nil
        repeats:Yes];

-(void)targetMethod:(NSTimer *)timer
{
    //Update label text

     NoOfStringsLeft --;

     if(NoOfStringsLeft == 0)
     {
         [timer invalidate];
     }      
}