自动布局动画以中心为锚点

Auto-Layout animation takes center as anchoring point

我有一个基于约束的高度动画。但是,当动画开始时,它会将视图的中心作为锚点而不是顶部。

我希望它的动画效果为固定顶部并从底部收缩。

抱歉我的英语不是母语。

我的观点主播:

top: superview
left: superview
right: superview
bottom: nil

// current height constant 200
view.height.constant = 0

UIView.animation(withDuration: 0.2, animations: {
    view.layoutIfNeded()
}}

更改代码

// current height constant 200
view.height.constant = 0

UIView.animation(withDuration: 0.2, animations: {
    view.layoutIfNeded()
}}

UIView.animate(withDuration: 2.0, animations: {
// current height constant 200
view.height.constant = 0
}}

希望对您有所帮助!

尝试代替您的观点:

superView.layoutIfNeeded()

这将布局子视图,您的动画应该可以正常工作。

所以替换

    // current height constant 200
view.height.constant = 0

UIView.animation(withDuration: 0.2, animations: {
    view.layoutIfNeded()
}

    // current height constant 200
yourView.height.constant = 0

UIView.animation(withDuration: 0.2, animations: {
    yourViewSuperview.layoutIfNeded()
}

查看 layoutIfNeeded() 苹果文档:[1]https://developer.apple.com/documentation/uikit/uiview/1622507-layoutifneeded