简体   繁体   English

AnimateWithDuration无法正常工作

[英]AnimateWithDuration doesn't work as expected

I'm trying to run an example to test this function. 我正在尝试运行一个示例来测试此功能。 I have a label an a button in the storyBoard and I have referenced the bottom constraint of the label in the view controller. 我在storyBoard中有一个标签和一个按钮,并且在视图控制器中引用了标签的底部约束。 When I use the button I want the label to move with an animation but it moves without it. 当我使用按钮时,我希望标签随动画一起移动,但没有动画就移动。 Here's the code of the button: 这是按钮的代码:

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

I know how to use it in swift but I'm having problems in objective c and I know it will be just for a little mistake... Any help? 我知道如何快速使用它,但是我在目标c中遇到问题,我知道这只是一个小错误……有什么帮助吗?

This is not the right way of animating a UI component constrained with Autolayout. 这不是对受自动版式约束的UI组件进行动画处理的正确方法。 You should first update the constraint, then call layoutIfNeeded within the animation block: 您应该首先更新约束,然后在动画块中调用layoutIfNeeded

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

Use this 用这个

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

Try this setNeedsLayout helps in some cases. 尝试使用此setNeedsLayout在某些情况下会有所帮助。

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

                  }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM