【问题标题】:Changing title of UIButton using animationWithDuration has no delay or animation使用 animationWithDuration 更改 UIButton 的标题没有延迟或动画
【发布时间】:2014-02-04 14:01:27
【问题描述】:

我有一个带有初始文本“开始”的开始按钮连接到 Interface Builder 中的一个操作。

但是,动画会立即开始和结束,而不是 3 + 5 秒。这段代码有什么问题?

@interface ViewController()
@property (weak, nonatomic) IBOutlet UIButton *startButton;
@end

@implementation ViewController

- (IBAction)startPressed:(UIButton *)sender
{
    [UIView animateWithDuration:3 delay:5
        options:UIViewAnimationOptionCurveEaseInOut
        animations:^{
            [self.startButton setTitle:@"New Title" forState:UIControlStateNormal];
        } 
        completion:nil];
}

@end

更新:当然,这些回答是正确的。我使用了 Jack Wu 的建议,虽然没有隐藏文本,但 setTitle 使按钮在屏幕上闪回。

- (IBAction)startPressed:(UIButton *)sender
{
    [UIView animateWithDuration:1.5 delay:5
        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{ self.startButton.alpha = 0; }
                     completion:^(BOOL c){ if (c) {
        self.startButton.hidden = YES;
        [self.startButton setTitle:@"newTitle" forState:UIControlStateNormal];
        [UIView animateWithDuration:1.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             self.startButton.hidden = NO;
                             self.startButton.alpha = 1;
                         }
                         completion:nil];

    }}];
}

【问题讨论】:

  • 你确定按钮的标题是动画的吗?
  • 我不确定你在这里期待什么......
  • 如果你想要某种过渡,也许你可以在 1.5 秒内淡出按钮,更改文本,然后再用 1.5 秒淡入?

标签: ios objective-c animation uibutton


【解决方案1】:

Title 不是 UIButton 的动画属性。正如其他人所建议的那样,您需要自己创建一个动画,方法是淡出按钮、更改标题并将其淡出。

您还可以编写自定义动画代码,获取按钮的位图快照,将其放在当前按钮的顶部,更改位图快照下的按钮标题,然后淡出并移除位图。这会给你一个交叉淡入淡出的效果,但这将是相当多的工作。

【讨论】:

    【解决方案2】:

    按钮的标题不是动画属性,因此您会看到它立即返回。这是设计使然。你可以在它的documentation 中找到 UIView 的动画属性。这是他们今天的情况。

    @property frame
    @property bounds
    @property center
    @property transform
    @property alpha
    @property backgroundColor
    @property contentStretch
    

    或者,您可以向按钮添加两个子视图标签,并在动画中交叉淡入淡出它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      相关资源
      最近更新 更多