UIView的高度约束动画

时间:2018-01-25 19:59:59

标签: ios swift animation uiview

我的UIView名为DetailView,具有以下约束:

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 个答案:

答案 0 :(得分:2)

动画主视图

 @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")
    }
}