ios - UILabel 动画淡入淡出不起作用

标签 ios objective-c core-animation uilabel fade

我正在尝试通过 UILabels 淡入和淡出来构建我的应用程序的简介。我有两个标签。我希望第一个淡入,在屏幕上停留 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];

}

最佳答案

使用基于 block 的动画并将动画链接在一起。所以有3个步骤。 label1 淡入,Label2 淡入,最后 Label3 淡入。我编写了下面的代码来淡入 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/

相关文章:

ios - 其他平台(iOS、Blackberry、Windows Mobile)上是否有等效的 AIDL

ios - 使用 RESTKit 将 url 参数映射到对象

ios - UIKit Dynamics 和 UIKit Animation 有什么区别?

ios - 访问 tableView 单元格内文本字段的 textfield.text

ios - 在 UITableViewCell 上点击时执行的条件 segue

iphone - 以编程方式创建新 View 并管理它们

ios - Objective C Completion Block 导致 swift 崩溃

iOS - 是否可以缓存 CGContextDrawImage?

macos - 基于 View 的 NSOutlineView 拖放突出显示不正确

ios - 某些图像未显示在 react-native-fastimage 中