ios - objective-C : animating height constraint change not working

标签 ios animation constraints autolayout

我有两个代码块,一个有效,第二个没有。首先添加一个 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;
                         }];
    };
  • 这一切都发生在主线程上。
  • 我尝试将框架的高度设置为 0,但也没有用
  • weakSelf.mandatoryPickupViewHeight是一个强大的属性,当我第二次激活时它不是零。

  • 有什么建议么?谢谢!

    最佳答案

    当您为动画使用自动布局时,您可以按以下方式进行操作:

  • 确保完成自动布局:
    self.view.layoutIfNeeded()
    
  • 然后在动画 block 之前更改约束。例如:
    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/

    相关文章:

    ios - 如何解决将应用程序上传到 ios 中的应用程序商店时出现的问题

    ios - 是否有 'trick' 用于在 NSURLSession 连接中发送原始 NSString 而不是 NSData?

    javascript - 弹出几秒钟后自动消失

    mysql - VARCHAR 中的 MariaDB CHECK 约束

    java - Swing : GridBagLayout adding components not the expected way

    c# - 检测泛型参数的 MaxValue

    ios - Firebase Chat App - 超时后停止输入指示器

    ios - 如何在不同的 iOS 设备上为 UINavigationBar 设置背景图片

    jQuery 动画在 IE 中不起作用

    ios - 动画图像动画RepeatCount CollectionViewCell