анимация высоты UIView с помощью автоматического размещения - PullRequest
0 голосов
/ 12 февраля 2020

Я хочу спросить, как я могу анимировать высоту UIView с помощью autolayout. мой первый вид, когда я коснулся его, расширяется, но второй и третий вид не расширяются. даже я пытаюсь распечатать его, но не печатать это мой код.

это мои настройки в xib, установите для приоритета middleContainer и bottomContainer низкий уровень. и имеют topConstraint константу для middleContainer и bottomContainer 25

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 Ответ

1 голос
/ 12 февраля 2020

Поток анимации должен быть как

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 
})
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...