动画自动布局 `leftAnchor` 约束变化

animating autolayout `leftAnchor` constraint change

我有一个 UIView,我正在对其进行初始化以在占 100% 的视图右侧开始 'offscreen'。我通过以下方式做到这一点:

[childView.leftAnchor constraintEqualToAnchor: superview.rightAnchor
                                     constant: 1.0].active = YES;

然后我想 'slide' 从左边进入视图。我想也许可以做以下事情:

[UIView animateWithDuration: 5
                 animations: ^{
                     [childView.leftAnchor constraintEqualToAnchor: superview.leftAnchor
                                                               constant: 1.0].active = YES;
                 }];

可能会这样做,但会立即发生(没有动画,只是 'appears')。

是否可以使用自动布局锚点来提供动画?

[childView.leftAnchor constraintEqualToAnchor: superview.leftAnchor constant: 1.0].active = YES;
[UIView animateWithDuration: 5
                 animations: ^{
                     [childView layoutIfNeeded];
                 }];

这应该可以做到。约束变化本身不应该在动画块中,而应该在布局调用中。您可能还应该 disable/remove 您所做的初始约束,因为这个新约束与其直接冲突(您可能会在控制台中看到警告)。