UIView的高度约束动画
Posted
技术标签:
【中文标题】UIView的高度约束动画【英文标题】:Height constraint animation of UIView 【发布时间】:2018-01-25 19:59:59 【问题描述】:我有名为 DetailView 的 UIView 具有以下约束:
DetailView.top = top
DetailView.trailing = Safe Area.trailing
DetailView.leading = Safe Area.leading
height = 295
全部在 StoryBoard 中制作。 DetailView is this gray rectangle on an image
我想在点击按钮后改变这个 UIView 的高度,所以我有 IBAction 来实现这个
@IBAction func detailViewTapped(_ sender: Any)
if detailViewHeightConstraint.constant < 500
detailViewHeightConstraint.constant = 523
UIView.animate(withDuration: 0.5)
self.detailView.layoutIfNeeded()
arrowImageView.image = UIImage(named: "up-arrow")
else
detailViewHeightConstraint.constant = 295
UIView.animate(withDuration: 0.5)
self.detailView.layoutIfNeeded()
arrowImageView.image = UIImage(named: "down-arrow")
改变尺寸效果很好,但动画效果不行。我想实现扩展到底部的效果,但是动画在屏幕之间开始一些东西,并且正在扩展到底部和顶部。 Start of animation can be seen on this picture怎样才能达到理想的效果?
【问题讨论】:
【参考方案1】:动画主视图
@IBAction func detailViewTapped(_ sender: Any)
if detailViewHeightConstraint.constant < 500
detailViewHeightConstraint.constant = 523
UIView.animate(withDuration: 0.5)
self.view.layoutIfNeeded()
arrowImageView.image = UIImage(named: "up-arrow")
else
detailViewHeightConstraint.constant = 295
UIView.animate(withDuration: 0.5)
self.view.layoutIfNeeded()
arrowImageView.image = UIImage(named: "down-arrow")
【讨论】:
以上是关于UIView的高度约束动画的主要内容,如果未能解决你的问题,请参考以下文章