Ios 设置自动布局“leftAnchor”约束更改的动画
我有一个UIView,我正在初始化它,以便在视图的右侧启动“屏幕外”视图,占100%。我通过以下方式来实现这一点:Ios 设置自动布局“leftAnchor”约束更改的动画,ios,objective-c,autolayout,nslayoutconstraint,Ios,Objective C,Autolayout,Nslayoutconstraint,我有一个UIView,我正在初始化它,以便在视图的右侧启动“屏幕外”视图,占100%。我通过以下方式来实现这一点: [childView.leftAnchor constraintEqualToAnchor: superview.rightAnchor constant: 1.0].active = YES; [UIView animateWithDuration: 5 animatio
[childView.leftAnchor constraintEqualToAnchor: superview.rightAnchor
constant: 1.0].active = YES;
[UIView animateWithDuration: 5
animations: ^{
[childView.leftAnchor constraintEqualToAnchor: superview.leftAnchor
constant: 1.0].active = YES;
}];
然后,我想从左侧“滑动”视图。我想也许可以做以下几点:
[childView.leftAnchor constraintEqualToAnchor: superview.rightAnchor
constant: 1.0].active = YES;
[UIView animateWithDuration: 5
animations: ^{
[childView.leftAnchor constraintEqualToAnchor: superview.leftAnchor
constant: 1.0].active = YES;
}];
可能会这样做,但它会立即发生(没有动画,它只是“出现”)
是否可以使用自动布局锚来提供动画
[childView.leftAnchor constraintEqualToAnchor: superview.leftAnchor constant: 1.0].active = YES;
[UIView animateWithDuration: 5
animations: ^{
[childView layoutIfNeeded];
}];
这应该可以做到。约束更改本身不应该在动画块中,而应该在布局调用中。您可能还应该禁用/删除您所做的初始约束,因为这个新约束与它直接冲突(您可能会在控制台中看到警告)