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

使用基于块的动画&将动画链接在一起。总共有3个步骤。label1 fadesIn, Label2 fadesIn,最后Label3 fadesIn。我已经为label1 &label2。淡出很简单。我想你可以把剩下的补上。从这里开始很直接…

Try this -

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

我建议使用块重写。首先是animateWithDuration:animations:completion:,然后是嵌套animateWithDuration:delay:options:animations:completion:。它要灵活得多,而且现在不需要在预块系统上运行。

而且,你写的第一个动画不会在4秒内触发

swift 4+

    UIView.animate(withDuration: 1.0, delay: 4, options: [.curveLinear, .allowUserInteraction], animations: {
    firstLabel.alpha = 1.0
}) { finished in
    if finished {
        UIView.animate(withDuration: 1.0, delay: 4.0, options: [.curveLinear, .allowUserInteraction], animations: {
            secondLabel.alpha = 1.0
        }) { finished in
            if finished {
                //put another block her to hide both the labels.
            }
        }
    }
}

最新更新