【问题标题】:AnimateWithDuration doesn't work as expectedAnimateWithDuration 无法按预期工作
【发布时间】:2017-05-12 12:51:35
【问题描述】:

我正在尝试运行一个示例来测试此功能。我在故事板中有一个标签和一个按钮,并且我在视图控制器中引用了标签的底部约束。当我使用按钮时,我希望标签随动画一起移动,但它在没有它的情况下移动。这是按钮的代码:

- (IBAction)sd:(id)sender {
[UIView animateWithDuration:0.5f animations:^{
    self.constraint.constant += 50;
}];
}

我知道如何快速使用它,但我在目标 c 中遇到问题,我知道这只是一个小错误......有什么帮助吗?

【问题讨论】:

    标签: ios objective-c animation


    【解决方案1】:

    这不是为受 Autolayout 约束的 UI 组件设置动画的正确方法。您应该先更新约束,然后在动画块中调用layoutIfNeeded

    self.constraint.constant += 50;
    [UIView animateWithDuration:0.5f animations:^{
      [self.view layoutIfNeeded];
    }];
    

    【讨论】:

      【解决方案2】:

      使用这个

       self.constraint.constant += 50;
          [UIView animateWithDuration:0.5f
              animations:^{
                  [self.view layoutIfNeeded]; 
              }];
      

      【讨论】:

        【解决方案3】:

        试试这个 setNeedsLayout 在某些情况下会有帮助。

        self.constraint.constant += 50;
        [UIView animateWithDuration:0.5f
                         animations:^{
                              [self.view setNeedsLayout];
                          }
                          completion:^(BOOL finished) {
        
                          }];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-12-23
          • 2014-12-09
          • 2016-01-13
          • 2020-09-21
          • 2011-08-17
          • 2012-04-29
          • 2021-08-12
          • 2019-02-04
          相关资源
          最近更新 更多