Как анимировать UIView, используя ограничение снизу вверх? - PullRequest
1 голос
/ 12 апреля 2019

Я создаю UIView программно, и после этого я буду анимировать это представление снизу вверх, используя ограничения. Но анимации не отображаются на этом объекте просмотра.

        overlay!.addSubview(contentView)
        contentView.translatesAutoresizingMaskIntoConstraints = false
        contentView.leadingAnchor.constraint(equalTo: vc.view.leadingAnchor, constant: 20).isActive = true
        contentView.trailingAnchor.constraint(equalTo: vc.view.trailingAnchor, constant: -20).isActive = true

        contentView.addSubview(messageLabel)
        messageLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8).isActive = true
        messageLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8).isActive = true
        messageLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8).isActive = true
        messageLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8).isActive = true

        let width: CGFloat = vc.view.frame.width - 40 - 16
        stringHeight = calculateEstimatedHeight(width: width, text: text) + 16
        contentView.heightAnchor.constraint(equalToConstant: stringHeight).isActive = true

        bottomConstraint = contentView.bottomAnchor.constraint(equalTo: vc.view.bottomAnchor, constant: stringHeight)
        bottomConstraint.isActive = true

        DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
            self.showToastViewWithAnimation(viewController: vc)
        }


@objc func showToastViewWithAnimation(viewController: Any) {
        if let vc = viewController as? ViewController {

            vc.view.layoutIfNeeded()

            UIView.animate(withDuration: 0.8) {
                self.bottomConstraint.constant = -20
                vc.view.layoutIfNeeded()
            }
        }
    }

Ответы [ 2 ]

0 голосов
/ 12 апреля 2019

Добавьте эту строку в ваш код:

self.translatesAutoresizingMaskIntoConstraints = false
contentView.translatesAutoresizingMaskIntoConstraints = false

, и в вашей функции ShowToastView сначала найдите тот, который является нижним ограничением, а затем попробуйте обновить его следующим образом (запомните, это код подсказки, вы можете обновить егосогласно вашему требованию):

@objc func showToastViewWithAnimation(viewController: Any) {
    if let vc = viewController as? ViewController {
        if let bottom = self.contentView.superview?.constraints.first(where: { (constraint) -> Bool in
        return constraint.firstAttribute == .bottom}){ 
// in this condition find your bottom constraint and update it, don't save your constraint in a variable.
           bottom.constant = -20 
           UIView.animate(withDuration: 0.8, animations: {               
             vc.view.layoutIfNeeded()
           }) { (complete) in

           }
        }
    }
}
0 голосов
/ 12 апреля 2019

Я указал несколько вещей в комментариях, прочитайте комментарий, и вы поймете, в чем проблема в вашем коде, проверьте код ниже.

=> ваш код: overlay!.addSubview(contentView)

   @objc func showToastViewWithAnimation(viewController: Any) {
            if let vc = viewController as? ViewController {
                //vc.view.layoutIfNeeded() - this is not required before updating constraint value
                self.bottomConstraint.constant = -20 //update contstraint outside of the animation block
                UIView.animate(withDuration: 0.8) {                
                    //vc.view.layoutIfNeeded() 
                    self.overlay?.layoutIfNeeded() //you needs to layout the overlay view, as your other toast views are added in overlay view and not in view of viewcontroller as per your code
                }
            }
        }
...