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

Tun*_*dır 1 ios autolayout ios-animations swift3

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

我希望它以固定顶部和从底部收缩的方式进行动画处理.

对不起,我的英语不是母语.

我的意见主播:

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

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

UIView.animation(withDuration: 0.2, animations: {
    view.layoutIfNeded()
}}
Run Code Online (Sandbox Code Playgroud)

Jes*_*ess 8

尝试而不是你的观点:

superView.layoutIfNeeded()
Run Code Online (Sandbox Code Playgroud)

这将布局子视图,你的动画应该工作.

所以更换

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

UIView.animation(withDuration: 0.2, animations: {
    view.layoutIfNeded()
}
Run Code Online (Sandbox Code Playgroud)

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

UIView.animation(withDuration: 0.2, animations: {
    yourViewSuperview.layoutIfNeded()
}
Run Code Online (Sandbox Code Playgroud)

请参阅layoutIfNeeded()apple docs:[1] https://developer.apple.com/documentation/uikit/uiview/1622507-layoutifneeded