【问题标题】:Changing text of UIButton titleLabel during alpha is 0.0f and avoid animation of text在 alpha 期间更改 UIButton titleLabel 的文本为 0.0f 并避免文本动画
【发布时间】:2013-06-30 23:31:36
【问题描述】:

我像这样淡入/淡出我的UIButton.titleLabel 几次:

[UIView beginAnimations:@"fadeOutText" context:NULL];
[UIView setAnimationDuration:1.0];
    button.titleLabel.alpha = 0.0f;
[UIView commitAnimations];

button.titleLabel.text = @"Changed text";

[UIView beginAnimations:@"fadeInText" context:NULL];
[UIView setAnimationDuration:1.0];
    button.titleLabel.alpha = 1.0f;
[UIView commitAnimations];

问题是我想在文本隐藏期间更改文本(alpha 为 0.0f)。 但是当我淡入文本时,文本也会被动画化。它从标签的右侧出现/移入。

有没有办法避免文本被动画化?

使用NSTimer 依次调用淡入和淡出。

谢谢!

【问题讨论】:

    标签: ios objective-c animation uibutton beginanimations


    【解决方案1】:

    试试这样:

    [UIView animateWithDuration:1.0
                             animations:^{
                                 button.titleLabel.alpha:0.0f;
                             }
                             completion:^(BOOL finished){
                                  button.titleLabel.text = @"Changed text";
                             }
    ];
    [UIView animateWithDuration:1.0
                             animations:^{
                                 button.titleLabel.alpha:1.0f;
                             }
    ];
    

    【讨论】:

      【解决方案2】:

      为文本更改添加单独的代码部分似乎会删除文本的动画。

      我是这样解决的,也许有更好的解决方案?

      -(void)hideText{
          [UIView beginAnimations:@"fadeOutText" context:NULL];
          [UIView setAnimationDuration:1.0];
              button.titleLabel.alpha = 0.0f;
          [UIView commitAnimations];
          [NSTimer scheduledTimerWithTimeInterval:1.0
                                           target:self
                                         selector:@selector(setText)
                                         userInfo:nil
                                          repeats:NO];
      }
      -(void)setText{
          [button setTitle:@"changedText" forState:UIControlStateNormal];
          [NSTimer scheduledTimerWithTimeInterval:0.1
                                       target:self
                                     selector:@selector(showText)
                                     userInfo:nil
                                      repeats:NO];
      }
      -(void)showText{
          [UIView beginAnimations:@"fadeInText" context:NULL];
          [UIView setAnimationDuration:1.0];
              button.titleLabel.alpha = 1.0f;
          [UIView commitAnimations];
      }
      

      【讨论】:

      • 问题是新标题设置了不同的框架,并且框架动画应用于标签本身。您的解决方法解决了这个问题,因为动画的上下文是分开的并按顺序执行
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多