3

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

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

对不起,我的英语不是母语人士。

我的观点锚定:

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

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

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

2 回答 2

10

尝试代替您的观点:

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

于 2017-07-27T07:44:49.357 回答
0

Change code

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

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

to

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

Hope it helps!

于 2017-07-27T07:32:32.103 回答