OGeek|极客世界-中国程序员成长平台

标题: ios - UILabel 动画淡入淡出不起作用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:19
标题: ios - UILabel 动画淡入淡出不起作用

我正在尝试使用 UILabel 淡入淡出来构建对我的应用程序的介绍。我有两个标签。我想让第一个淡入,在屏幕上停留 4 秒钟。然后第二个标签应该淡入并在屏幕上停留 4 秒钟。然后它应该淡出两个标签。

我有以下代码,但它没有做任何事情,因为它直接进入最终状态。我在 viewDidAppear() 中有以下方法。我做错了什么?

-(void) animateLabels
{
    [UIView beginAnimations"First Label Display" context:nil];
    [UIView setAnimationDelay:4.0];
    firstLabel.alpha = 1;
    [UIView commitAnimations];


    [UIView beginAnimations"Second Label Display" context:nil];
    [UIView setAnimationDelay:6.0];
    secondLabel.alpha = 1;
    [UILabel commitAnimations];

    [UIView beginAnimations"Hide Labels" context:nil];
    [UIView setAnimationDelay:10.0];
    secondLabel.alpha = 0;
    firstLabel.alpha=0;
    [UILabel commitAnimations];

}



Best Answer-推荐答案


使用基于 block 的动画并将您的动画链接在一起。所以有3个步骤。标签1 淡入,标签2 淡入,最后标签3 淡入。我已经编写了下面的代码来淡入 label1 和 label2。淡出很简单。我想你可以填写其余的。从这里直接开始......

试试这个 -

[UIView animateWithDuration:1.0 
                      delay:4 
                    options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                 animations:^(void) 
 {
     [firstLabel setAlpha:1.0];
 } 
                 completion:^(BOOL finished) 
 {
     if(finished)
     {
         [UIView animateWithDuration:1.0 
                               delay:4.0 
                             options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                          animations:^(void) 
          {
              [secondLabel setAlpha:1.0];
          } 
                          completion:^(BOOL finished) 
          {
              if(finished)
              {
                  //put another block her to hide both the labels.
              }
          }];
     }
 }];

关于ios - UILabel 动画淡入淡出不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8216549/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4