用自动布局动画UIView高度

时间:2020-02-12 12:31:26

标签: swift autolayout

我想问一下如何用autolayout为UIView高度设置动画。当我点击它时,我的第一个视图会扩展,但是第二个和第三个视图不会扩展。即使我尝试打印它,仍然不打印这是我的代码。

这是我在xib中的设置,将middleContainerbottomContainer的优先级设置为低。并为topConstraintmiddleContainer 25设置bottomContainer常量

enter image description here

@IBOutlet weak var topContainerHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var middleContainerHeightConstraint: NSLayoutConstraint!

    @IBOutlet weak var bottomContainerHeightConstraint: NSLayoutConstraint!
    @IBOutlet weak var tableViewMiddleConstraint: NSLayoutConstraint!
    @IBOutlet weak var tableViewBottomConstraint: NSLayoutConstraint!

    override func awakeFromNib() {
        super.awakeFromNib()

        calendarView.alpha = 0
        let topTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTopTap))
        let middleTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleMiddleTap))
        let bottomTapGesture = UITapGestureRecognizer(target: self, action: #selector(handlebottomTap))
        topContainer.addGestureRecognizer(topTapGesture)
        middleContainer.addGestureRecognizer(middleTapGesture)
        bottomContainer.addGestureRecognizer(bottomTapGesture)
    }

    @objc func handleTopTap(gesture: UITapGestureRecognizer) {
//        print("Tapped")
        if topContainerHeightConstraint.constant == 75 {
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                self.arrowImageView.image = #imageLiteral(resourceName: "up-chevron")
                self.topContainerHeightConstraint.constant = 350
                self.calendarView.alpha = 1
            })
        } else {
            defaultConstraint()
        }
    }

    @objc func handleMiddleTap(gesture: UITapGestureRecognizer) {
        print("Tapped")
        if middleContainerHeightConstraint.constant == 75  {
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                self.arrowImageView.image = #imageLiteral(resourceName: "up-chevron")
                self.middleContainerHeightConstraint.constant = 110
                self.tableViewMiddleConstraint.constant = 110
            })
        } else {
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                self.arrowImageView.image = #imageLiteral(resourceName: "up-chevron")
                self.middleContainerHeightConstraint.constant = 75
                self.tableViewMiddleConstraint.constant = 0
            })
        }
    }

    @objc func handlebottomTap(gesture: UITapGestureRecognizer) {
        if bottomContainerHeightConstraint.constant == 75 {
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                self.arrowImageView.image = #imageLiteral(resourceName: "up-chevron")
                self.bottomContainerHeightConstraint.constant = 110
                self.tableViewBottomConstraint.constant = 162
            })
        } else {
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                self.arrowImageView.image = #imageLiteral(resourceName: "up-chevron")
                self.bottomContainerHeightConstraint.constant = 75
                self.tableViewBottomConstraint.constant = 0
            })
        }
    }

    fileprivate func defaultConstraint() {
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            self.arrowImageView.image = #imageLiteral(resourceName: "down-chevron")
            self.topContainerHeightConstraint.constant = 75
            self.calendarView.alpha = 0
            self.calendarView.layoutIfNeeded()
        })
    }

你能帮我哪里做错了吗?

1 个答案:

答案 0 :(得分:1)

动画流程应该像

self.topContainerHeightConstraint.constant = 75  // 1 change constant
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 1, options: .curveEaseOut, animations: { 
    self.layoutIfNeeded() // 2 layout superView 
})