Objective-C:动画化高度约束更改不起作用 [英] Objective-C: animating height constraint change not working

查看:110
本文介绍了Objective-C:动画化高度约束更改不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个代码块,其中一个有效,而第二个则不是.首先添加一个视图,将其高度限制为0,然后在动画块中取消此限制,使其长大.完美的作品.第二个应该重新激活0 height constraint,以使它的高度缩小到0,然后将其删除,而是立即将其删除.这是代码:

I have two blocks of code, one working and the second not really. The first adds a view, constraints its height to 0 and then cancel this constraint in an animation block so it grows up. Works perfect. The second one is supposed to reactivate the 0 height constraint so that its height shrinks back to 0 and then remove it, but instead it is removed instantaneously. Here's the code:

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;
                         }];
    };

  1. 这一切都发生在主线程上.
  2. 我尝试将框架的高度设置为0,但是也没有用
  3. weakSelf.mandatoryPickupViewHeight是一个很强的属性,当我第二次激活它时,它并不为零.
  1. It's all happening on the main thread.
  2. I tried settings the frame's height to 0 instead, also didn't work
  3. weakSelf.mandatoryPickupViewHeight is a strong property and it is not nil when I activated the second time.

有什么建议吗?谢谢!

推荐答案

在对动画使用自动布局时,请按以下步骤操作:

When you are using autolayout for animations, you do it as follows:

  1. 确保完成自动布局:

  1. Make sure autolayout is done:

self.view.layoutIfNeeded()

  • 然后在动画块之前更改约束.例如:

  • Then you change the constraints BEFORE the animation block. So for example:

    someConstraint.constant = 0
    

  • 然后,在更改约束后,您告诉自动布局,约束已被更改:

  • Then after changing the constraint, you tell the autolayout that constraints have been changed:

    self.view.setNeedsLayout()
    

  • 然后您只需调用layoutIfNeeded()即可添加动画块:

  • And then you add an animation block with simply calling layoutIfNeeded():

    UIView.animate(withDuration: 1, animations: {
        self.view.layoutIfNeeded()
    })
    

  • 请注意,您没有停用约束-它必须一直处于活动状态.

    Notice that you don't deactivate the constraint - it has to be active the whole time.

    我想您的代码中的问题在这里:

    I guess that in your code the problem is here:

        [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约束与这些约束没有冲突吗?

    Although what you are doing seems a bit obscure. If the constraint is deactivated, are you sure the autolayout has enough constraints to calculate the proper height? If so, are you sure that mandatoryPickupViewHeight constraint is not in collision with those?

    这篇关于Objective-C:动画化高度约束更改不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆