我有两个代码块,一个有效,第二个没有。首先添加一个 View ,将其高度限制为 0,然后在动画 block 中取消此约束,使其长大。完美运行。第二个应该重新激活 0 height constraint
使其高度收缩回 0 然后将其删除,但它会立即被删除。这是代码:
self.pickupAddressViewModel.infoViewBlock = ^(MandatoryPickupView * _Nonnull pickupView) {
if (weakSelf.mandatoryPickupView) {
return;
}
weakSelf.mandatoryPickupView = pickupView;
[weakSelf.view addSubview:pickupView];
CGFloat gap = weakSelf.orderButton.originY;
[[pickupView.bottomAnchor constraintEqualToAnchor:weakSelf.view.bottomAnchor constant:-gap] setActive:YES];
[[pickupView.leadingAnchor constraintEqualToAnchor:weakSelf.view.leadingAnchor
constant:16.0] setActive:YES];
[[pickupView.trailingAnchor constraintEqualToAnchor:weakSelf.view.trailingAnchor
constant:-16.0] setActive:YES];
weakSelf.mandatoryPickupViewHeight = [pickupView.heightAnchor constraintEqualToConstant:0];
[weakSelf.mandatoryPickupViewHeight setActive:YES];
[weakSelf.view layoutIfNeeded];
[UIView animateWithDuration:0.5 animations:^{
[weakSelf.mandatoryPickupViewHeight setActive:NO];
[weakSelf.view layoutIfNeeded];
}];
};
self.pickupAddressViewModel.closeViewBlock = ^{
if (!weakSelf.mandatoryPickupView) {
return;
}
[weakSelf.view layoutIfNeeded];
[UIView animateWithDuration:10.5
animations:^{
[weakSelf.mandatoryPickupViewHeight setActive:YES];
[weakSelf.view layoutIfNeeded];
} completion:^(BOOL finished) {
[weakSelf.mandatoryPickupView removeFromSuperview];
weakSelf.mandatoryPickupView = nil;
weakSelf.mandatoryPickupViewHeight = nil;
}];
};
weakSelf.mandatoryPickupViewHeight
是一个强大的属性,当我第二次激活时它不是零。 有什么建议么?谢谢!
最佳答案
当您为动画使用自动布局时,您可以按以下方式进行操作:
self.view.layoutIfNeeded()
someConstraint.constant = 0
self.view.setNeedsLayout()
layoutIfNeeded()
添加一个动画 block :UIView.animate(withDuration: 1, animations: {
self.view.layoutIfNeeded()
})
请注意,您没有停用约束 - 它必须始终处于事件状态。
我想在你的代码中问题出在:
[UIView animateWithDuration:0.5 animations:^{
[weakSelf.mandatoryPickupViewHeight setActive:NO];
[weakSelf.view layoutIfNeeded];
}];
尝试将其更改为
[weakSelf.mandatoryPickupViewHeight setActive:NO];
[weakSelf.view setNeedsLayout];
[UIView animateWithDuration:0.5 animations:^{
[weakSelf.view layoutIfNeeded];
}];
虽然你在做什么似乎有点模糊。如果约束被禁用,您确定自动布局有足够的约束来计算正确的高度吗?如果是,你确定
mandatoryPickupViewHeight
约束是不是和那些冲突?
关于ios - objective-C : animating height constraint change not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48091661/