简体   繁体   English

制作动画时无法同时满足约束

[英]Unable to simultaneously satisfy constraints when animating

I have six user constraints setup with IB that look like this: 我有六个用户约束设置与IB,如下所示:

H:|-(593)-[UIView(411)]-(20)-|
V:|-(20)-[UIView(288)]-(396)-|

I grow and shrink the view by changing the constraints and then calling layoutIfNeeded. 我通过更改约束然后调用layoutIfNeeded来扩大和缩小视图。 For example, to grow the view I will do: 例如,要扩大视图,我将要做:

H:|-(20)-[UIView(984)]-(20)-|
V:|-(20)-[UIView(663)]-(20)-|

And then call 然后打电话

[UIView animateWithDuration:.5 animations:^{
    [self.view layoutIfNeeded];
}];

This technique grows and shrinks my view, and it looks nice, but I'm given a rather confusing warning: 这种技术增长并缩小了我的观点,它看起来不错,但我给了一个相当混乱的警告:

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x148d5af0 H:[UIView:0x148d4e50(411)]>",
    "<NSLayoutConstraint:0x148cc940 H:[UITableView:0xace7600(319)]>",
    "<NSLayoutConstraint:0x148ce040 H:|-(NSSpace(20))-[UITableView:0xacd4e00]   (Names: '|':UIView:0x148cddd0 )>",
    "<NSLayoutConstraint:0x148cdf00 H:[UITableView:0xace7600]-(NSSpace(20))-|   (Names: '|':UIView:0x148cddd0 )>",
    "<NSLayoutConstraint:0x148cdea0 H:[UITableView:0xacd4e00]-(NSSpace(8))-[UITableView:0xace7600]>",
    "<NSLayoutConstraint:0x148d4c10 UIView:0x148cddd0.trailing == UIView:0x148cdd40.trailing>",
    "<NSLayoutConstraint:0x148d4b90 H:|-(0)-[UIView:0x148cddd0]   (Names: '|':UIView:0x148cdd40 )>",
    "<NSLayoutConstraint:0x148d6020 H:|-(320)-[UIView:0x148cdd40]   (Names: '|':UIView:0x148cd330 )>",
    "<NSLayoutConstraint:0x148d5fa0 UIView:0x148cdd40.trailing == UIView:0x148cd330.trailing>",
    "<NSLayoutConstraint:0x148d5f60 H:[UIView:0x148d4e50]-(NSSpace(20))-|   (Names: '|':UIView:0x148cd330 )>",
    "<NSLayoutConstraint:0x148d5ee0 H:|-(20)-[UIView:0x148d4e50]   (Names: '|':UIView:0x148cd330 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x148cc940 H:[UITableView:0xace7600(319)]>

Now all of these constraints are generated by IB. 现在,所有这些约束都由IB生成。 I've double^(triple!) checked this. 我已经加倍^(三倍!)检查了一下。 I've put these constraints together with a pen and paper and have gotten this: 我把这些限制与笔和纸放在一起,得到了这个:

UIView_A H:[-(20)-[UIView_E]-(20)-] and H:[-(320)-(UIView_B)
UIView_B H:[-(0)-[UIView_D]
UIView_C H:[UIView_C(411)]
UIView_D H:[-(20)-[UITableView_F]-[UITableView_G(319)]-(20)-]

I don't understand how these constraints can't be satisfied. 我不明白如何无法满足这些约束。 They look fine. 他们看起来很好。 I don't change them and they're generated by IB. 我不会更改它们,它们是由IB生成的。 Aren't constraints generated by IB automatically satisfiable? IB产生的约束条件不能自动满足吗?

Or, at a minimum, is there a way to stop warnings? 或者,至少有没有办法停止警告? It behaves perfectly and I don't need to see that it's breaking a constraint that doesn't seem to do anything anyway. 它表现得很完美,我不需要看到它打破了一个似乎无论如何都没有做任何事情的约束。

This constraint: 这个约束:

H:[UITableView:0xace7600(319)]>" H:[UITableView的:0xace7600(319)]>”

seems to be an impediment for the system resolution. 似乎是系统分辨率的障碍。

Can you remove it ? 可以删除吗?

So, it turns out, the order at which I change constraints matters. 事实证明,我改变约束的顺序很重要。

To grow the view, I will 为了增长观点,我会

  1. Increase the width: H:|-(593)-[UIView( 984 )]-(20)-| 增加宽度:H:| - (593) - [UIView( 984 )] - (20) - |
  2. Decrease the leading space: H:|-( 20 )-[UIView(984)]-(20)-| 减少领先空间:H:| - ( 20 ) - [UIView(984)] - (20) - |

This produces no warnings. 这不会产生任何警告。 However, if I do this in the opposite order, I'll get a warning: 但是,如果我以相反的顺序执行此操作,我会收到警告:

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints ...

...Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1567f650 H:[UITableView:0x119e6200(319)]>

When shrinking the view, I copied the code that increased the width (with the same order) and just changed the values. 缩小视图时,我复制了增加宽度的代码(具有相同的顺序),只是更改了值。 That gave me the warning I posted in my original question. 这给了我在原始问题中发布的警告。 When I switched the order for shrinking, the warnings disappeared. 当我改变订单收缩时,警告消失了。


Why is this the case? 为什么会这样? I don't know. 我不知道。 I'll update as I discover more. 当我发现更多时,我会更新。

I got same message, and I finally got it why it happens, 我得到了同样的信息,我终于明白了为什么会这样,
My solution is: Do not let any object FLIP OVER during at any moment of the animation. 我的解决方案是:在动画的任何时刻都不要让任何对象FLIP OVER。

In other words, contraints are should be OUTside of any object, but sometimes constraints are inside during animation, not like our expection. 换句话说,约束应该是任何对象的超越,但有时在动画期间约束在内部,而不是像我们的预期。

In another words, do not let top margin invade bottom margin due to constraint animation. 换句话说,不要让顶部边距因约束动画而侵入底部边距。

For example, 例如,

     top constraint: topA = initially 100

                    [Box A]

     bottom constraint: botA = initially 150

now, if you set like below and animate, 现在,如果您设置如下并进行动画处理,

      topA = 300
      botA = 25  

then error should occur, why: thread invades the bottom margin BEFORE bottom margin go down. 然后应该出现错误,原因:线程在底部边距下降之前侵入底部边距。 So, You'd rather change the ORDER, 因此,您宁愿更改订单,

      botA = 25
      topA = 300

Then error will disappear because bottom constraint would preserve the height of the mass and next top constraint would shrink the object's height not invading the bottom margin. 然后错误将消失,因为底部约束将保留质量的高度,而下一个顶部约束将收缩对象的高度而不会侵入底部边距。

*Point: Let the object have width and height greater than 0 CONTINUALLY even during animation, not interrupted by constraints change. *点:即使在动画期间,也要使对象的宽度和高度大于0,并且不会因约​​束更改而中断。

I hope I helped you. 我希望我帮助过你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM