ios - UIView的高度约束动画

标签 ios swift animation uiview

我有一个名为 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怎样才能达到理想的效果?

最佳答案

动画主视图

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

关于ios - UIView的高度约束动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48450959/

相关文章:

ios - 将应用上传到应用商店后的状态 “Missing Screenshot [1]”

iOS UILabel.appearance().textColor 不可用

ios - 使用 UIView.transition() 方法的幻灯片过渡

java - Android : java. lang.OutOfMemoryError:

ios - plist 文件会不会太大?

ios - Swift:防止 NSTimer 自动启动

ios - 类型不符合协议(protocol) 'Encodable'

swift - 嵌套枚举以便在 Swift 中通过 switch 语句访问的更简洁的方法?

arrays - 将数组传递给 Swift 中的函数抛出无效参数类型错误

javascript - 如何验证页面转换?