【问题标题】:UILabel animated fade in out isn't workingUILabel 动画淡入淡出不起作用
【发布时间】:2011-11-21 18:22:04
【问题描述】:

我正在尝试使用 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];

}

【问题讨论】:

标签: ios objective-c core-animation uilabel fade


【解决方案1】:

使用基于块的动画并将您的动画链接在一起。所以有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 3.x 吗?还是我不应该费心支持运行 iOS 3.x 的设备?
  • 这是基于块的动画。我不认为它会在 iOS3.X 中工作,这很棘手。最简单的事情是不支持 iOS3 及以下。但是,如果您打算这样做,则需要使用您在问题中发布的更复杂的 2block 方法来编写此动画。我建议你坚持支持> iOS4.X
【解决方案2】:

我建议使用块重写它。首先是animateWithDuration:animations:completion:,然后是嵌套的animateWithDuration:delay:options:animations:completion:。它更加灵活,现在无需在预块系统上运行。

此外,您编写的第一个动画在 4 秒内不会触发。

【讨论】:

    【解决方案3】:

    这是 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.
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-06
      • 2012-09-25
      • 2019-09-24
      • 1970-01-01
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多