与自动版式动画 [英] Animate with autolayout

查看:131
本文介绍了与自动版式动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的观点堆叠这样

I have a series of view stacked this way

 ________________
|                |
|     View 1     |
|________________|
|                |
|     View 2     |
|________________|
|                |
|     View 3     |
|________________|

这些视图可以展开和折叠,所以如果查看1膨胀,查看2有其顶视图的底部,同样为与视图2相关的视图。

These view can be expanded and collapsed so if View 1 is expanded, View 2 has its top an the bottom of View 1, and the same for the View 3 related with the View 2.

 ________________
|                |
|     View 1     |
|                |
|                |
|                |
|________________|
|                |
|     View 2     |
|________________|
|                |
|     View 3     |
|________________|

 ________________
|                |
|     View 1     |
|________________|
|                |
|     View 2     |
|                |
|                |
|                |
|________________|
|                |
|     View 3     |
|________________|

我不能因为这个布局创建通过IB添加这些视图dinamycally所以我必须通过增加code和制约的看法了。

I can't add these view via IB because this layout is created dinamycally so I have to add the views via code and the constraints too.

我这样做

UIView *previousView = nil;
for (UIView *view in views) {

    if (previousView) {

        NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[previousView][view]"
                                                                       options:0
                                                                       metrics:nil
                                                                         views:NSDictionaryOfVariableBindings(previousView, view)];

        [superview addConstraints:constraints];
    }
}

当我点击以便展开我收到一个错误

When I tap a view to expand it I'm receiving an error

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) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x76407b0 h=&-& v=--& V:[MyView:0x764c0f0(44)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x763e370 h=&-& v=--& V:[MyView:0x7646490(44)]>",
    "<NSLayoutConstraint:0x76440d0 V:[MyView:0x764c0f0]-(0)-[MyView:0x7648eb0]>",
    "<NSLayoutConstraint:0x7643920 V:[MyView:0x7648eb0]-(0)-[MyView:0x7646490]>",
    "<NSAutoresizingMaskLayoutConstraint:0x76407f0 h=&-& v=--& MyView:0x764c0f0.midY == + 22>",
    "<NSAutoresizingMaskLayoutConstraint:0x76d9ea0 h=&-& v=--& MyView:0x7648eb0.midY == + 91.5>",
    "<NSAutoresizingMaskLayoutConstraint:0x763e3b0 h=&-& v=--& MyView:0x7646490.midY == + 110>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7643920 V:[MyView:0x7648eb0]-(0)-[MyView:0x7646490]>

显然,我这样做是错误的。我一定要设置 setTranslatesAutoresizingMaskIntoConstraints NO 并添加自己的约束来定位?或者问题是我添加上环约束?

Obviously I'm doing this wrong. Do I have to set setTranslatesAutoresizingMaskIntoConstraints to NO and add my own constraints to the positioning? Or the problem is the constraint that I'm adding on that loop?

推荐答案

你是如何改变视图的大小?你有一个约束为0固定你的两个观点之间的垂直空间就是你得到你展示的任何其他约束(你没有明确添加也许那些)的错误?我怀疑可能有上查看2和/或查看3另一个制约因素是不允许他们下移,以满足 V- [查看1] - (0) - [查看2] 约束。

How are you changing the size of the view? You have a constraint fixing the vertical space between your two views at 0. Is the error you get showing you any other constraints (perhaps ones that you didn't add explicitly)? I suspect that there's probably another constraint on View 2 and/or View 3 which isn't allowing them to move down to satisfy the V-[View 1]-(0)-[View 2] constraint.

如果没有其他限制参与,只需更改要展开或折叠应该工作的观点的高度。

If there were no other constraints involved, simply changing the height of the view you want to expand or collapse should work.

如在<一个提到href=\"https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/AutolayoutPG/Articles/adoption.html\"相对=nofollow>如果你设置苹果文档的所有约束自己你应该叫 setTranslatesAutoresizingMaskIntoConstraints:NO ,否则您的明确限制将与冲突在自动隐式添加蒙版的限制,它看起来像什么在这种情况下发生的事情。

As mentioned in the Apple documentation if you're setting the all the constraints yourself you should probably call setTranslatesAutoresizingMaskIntoConstraints:NO otherwise your explicit constraints will conflict with the Autoresizing Mask constraints added implicitly, which looks like what's happening in this case.

编辑:在看到上面列出了所有的约束你的编辑,这印证了我在说什么。通过翻译自动调整大小面具与你的汽车布局约束矛盾的添加约束。它们被明确地钉扎相对于上海华每个视图的中心Y位置。尝试 setTranslatesAutoresizingMaskIntoConstraints:NO 并设置您需要按照规定的任何其他限制

having seen your edit above listing all the constraints, it confirms what I'm saying. The constraints being added by translating the auto-resizing masks are conflicting with your auto layout constraint. They are explicitly pinning the center Y position of each view in relation to the superview. Try setTranslatesAutoresizingMaskIntoConstraints:NO and set any other constraints that you need as required.

这篇关于与自动版式动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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