【发布时间】:2018-08-09 00:02:13
【问题描述】:
Xcode Swift 4
我有一些在代码中动态添加的视图。 每个新的视图顶部锚点都连接到先前的视图底部锚点。 每个视图都有一个按钮,可以使视图随动画展开/折叠。这是按钮代码:
let fullHeight : CGFloat = 240
let smallHeight : CGFloat = 44
let currentHeigth = rootView.frame.size.height //I use this to get current height and understand expanded view or not
let heighCons = rootView.constraints.filter //I use this to deactivate current height Anchor constraint
{
$0.firstAttribute == NSLayoutAttribute.height
}
NSLayoutConstraint.deactivate(heighCons)
rootView.layoutIfNeeded()
if currentHeigth == smallHeight
{
rootView.heightAnchor.constraint(equalToConstant: fullHeight).isActive = true
rootView.setNeedsLayout()
UIView.animate(withDuration: 0.5)
{
rootView.layoutIfNeeded() //animation itself
}
}
else
{
rootView.heightAnchor.constraint(equalToConstant: smallHeight).isActive = true
rootView.setNeedsLayout()
UIView.animate(withDuration: 0.5)
{
rootView.layoutIfNeeded() //animation itself
}
}
这一切都很完美,但我有一个问题:查看当前扩展视图下方的视图会立即更改它的 y 位置,而没有动画。它只是跳转到上一个视图底部锚点,在动画完成后将处于活动状态。
所以我的问题是:
1) 当视图通过底部/顶部动画相互连接时,制作高度约束动画的正确方法是什么?
2) 我的目标只是制作一个可以在单击按钮时展开/折叠的视图,也许我应该以另一种方式来做?
【问题讨论】:
标签: xcode animation constraints swift4