简体   繁体   English

使用autolayout为包含子视图的视图设置动画

[英]Animate a view containing subviews with autolayout

This is the setup: 这是设置:

  • A UIView created on Interface Builder, linked to an IBOutlet variable ( _vAbout ) 在Interface Builder上创建的UIView,链接到IBOutlet变量( _vAbout
  • A constraint for this view that we want to animate, linked to an IBOutlet variable ( _ctrBottomAboutView ) 我们想要动画的这个视图的约束,链接到IBOutlet变量( _ctrBottomAboutView

I am using this code to animate: 我正在使用此代码进行动画处理:

_ctrBottomAboutView.constant = -100;
[UIView animateWithDuration:0.5 animations:^{
    [_vAbout layoutIfNeeded];
}

My problem is: whenever the view has any subviews in it, the animation doesn't work . 我的问题是: 只要视图中有任何子视图,动画就不起作用 However, if the view has no children, the animation works correctly. 但是,如果视图没有子项,则动画可以正常工作。

Do you have any idea of a solution? 你知道解决方案吗? I have tried everything: adding and removing constraints instead of modificating the constant value, adding constraints to the subviews on Interface Builder... 我已经尝试了一切:添加和删除约束而不是修改常量值,在Interface Builder上向子视图添加约束...

After some experiments starting from the ground with an empty project, this is what I've found: 经过一些空的项目从地面开始的一些实验,这是我发现的:

Given A the view we want to animate and B its superview 鉴于我们希望动画的视图和B的超级视图

  • It's very important to keep in mind that the view that receives the layoutIfNeeded message is the view that owns the constraint . 请记住,接收layoutIfNeeded消息的视图是拥有约束的视图,这一点非常重要。
  • In the case of NSLayoutAttributeWidth and NSLayoutAttributeHeight the owner of the constraint is actually A, but in all the other cases, the view that owns the constraint is B NSLayoutAttributeWidthNSLayoutAttributeHeight的情况下,约束的所有者实际上是A,但在所有其他情况下,拥有约束的视图是B

HOWEVER 然而

  • If A does not have any subviews, we can call [A layoutIfNeeded] at any time on our code and it will work 如果A没有任何子视图,我们可以随时在我们的代码上调用[A layoutIfNeeded] ,它会工作
  • If A has one or more subviews but we start the animation on viewDidLoad we can call [A layoutIfNeeded] and it will work 如果A有一个或多个子视图,但我们在viewDidLoad上启动动画,我们可以调用[A layoutIfNeeded] ,它会工作

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

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