简体   繁体   English

如何在iOS中管理可移动视图的约束?

[英]How to manage constraints of movable views in iOS?

I've a set of buttons which I animate in expand-collapse fashion, more like a drawer open and close action. 我有一组按钮,它们以展开-折叠方式进行动画处理,更像是抽屉的打开和关闭动作。 I simply vary the X positions of these buttons to achieve this and it works fine. 我只需改变这些按钮的X位置即可实现此目的,并且效果很好。 While designing this layout in interface builder, I've set the constraints for all these buttons in their ' drawer-closed ' state. 在界面生成器中设计此布局时,我将所有这些按钮的约束设置为“ 抽屉关闭 ”状态。

Problem arises when the drawer is left open and some other view on the screen gets updated. 当抽屉保持打开状态并且屏幕上的其他视图更新时,会出现问题。 I understand that viewDidLayoutSubviews() gets called at this instance. 我了解到viewDidLayoutSubviews()在此实例中被调用。 Because of this, the drawer menu which was left open closes abruptly messing up with the 'drawer close' animation and the buttons don't get the expected view as in their 'drawer close' state. 因此,一直处于打开状态的抽屉菜单会突然关闭,并与“抽屉关闭”动画混淆,并且按钮在“抽屉关闭”状态下无法获得预期的视图。

I suppose the constraints of the buttons would be getting re-initialized when the viewDidLayoutSubviews() got called. viewDidLayoutSubviews()被调用时,按钮的约束将被重新初始化。

Now coming to my question - how do we set/manage constraints for views which are going to move runtime because of animations? 现在要问我的问题-我们如何为由于动画而移动运行时的视图设置/管理约束?

I'm a beginner in iOS app development and I did go through questions asked on managing constraints programmatically or viewDidLayoutSubviews etc. but I couldn't find a standard way to follow in the situation I've encountered. 我是iOS应用程序开发的初学者,确实遇到了有关以编程方式管理约束viewDidLayoutSubviews等问题,但是我找不到遇到这种情况的标准方法。

在此处输入图片说明

Instead of changing the frame, you could animate the change of constraints. 可以更改约束的动画,而不用更改框架。

Short preview of how to do that: 简短预览:

yourConstraint.constant = newValue

UIView.animate(withDuration: animationDuration) {
    yourView.layoutIfNeeded()
}

More information about this can be found here . 有关此的更多信息,请参见此处

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

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