OGeek|极客世界-中国程序员成长平台

标题: ios - AnimateWithDuration 无法按预期工作 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:26
标题: ios - AnimateWithDuration 无法按预期工作

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

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

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



Best Answer-推荐答案


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

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

关于ios - AnimateWithDuration 无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43938237/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4