提问者:小点点

为什么我的UIView动画在添加了子视图之后就不工作了?


我正在做一个从屏幕上方滑出的祝酒词,如下所示:

但是当我添加uilabel作为toast容器的子视图时,toast将不再动画和显示。下面是我的showtoast()函数:

func showToast(message: String) {
    let screenSize = UIScreen.main.bounds.size
    let containerView = UIView()
    containerView.translatesAutoresizingMaskIntoConstraints = false
    containerView.backgroundColor = UIColor.black.withAlphaComponent(0.6)
    containerView.alpha = 1.0
    containerView.clipsToBounds = true
    containerView.layer.cornerRadius = 10
    containerView.frame = CGRect(x: 0, y: 0, width: screenSize.width - 16, height: 50)
    containerView.center.x = view.center.x
    // If I add this UILabel as a subview the animation doesn't work
    let toastLbl = UILabel()
    toastLbl.translatesAutoresizingMaskIntoConstraints = false
    containerView.addSubview(toastLbl)
    toastLbl.textColor = UIColor.white
    toastLbl.font = .regularMontserrat(ofSize: 14)
    toastLbl.textAlignment = .center
    toastLbl.text = message
    toastLbl.numberOfLines = 1
    // If I add this UILabel as a subview the animation doesn't work
    view.addSubview(containerView)
    
    UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
        containerView.frame.origin.y = 50
    }, completion: { _ in
        UIView.animate(withDuration: 0.5, delay: 1.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
            containerView.frame.origin.y = -50
        }, completion: { _ in
            containerView.removeFromSuperview()
        })
    })
}

有办法解决这个问题吗?


共1个答案

匿名用户

现在我再次尝试使用自动布局,它和我预期的一样工作。我想我给约束常量放了一个错误的值,所以它不能正常工作。谢谢@gereon的建议。

我将showtoast()函数分为两个独立的函数,如下所示:

func makeToast(message: String) -> UIView {
        let screenSize = UIScreen.main.bounds.size
        let containerView = UIView()
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.backgroundColor = .CREAM_ORANGE
        containerView.alpha = 1.0
        containerView.clipsToBounds = true
        containerView.layer.cornerRadius = 10
        containerView.frame = CGRect(x: 0, y: 0, width: screenSize.width - 32, height: 50)
        containerView.center.x = view.center.x
        containerView.alpha = 0
        let toastLbl = UILabel()
        toastLbl.translatesAutoresizingMaskIntoConstraints = false
        toastLbl.textColor = UIColor.white
        toastLbl.font = .regularMontserrat(ofSize: 14)
        toastLbl.textAlignment = .center
        toastLbl.text = message
        toastLbl.numberOfLines = 1
        view.addSubview(containerView)
        containerView.addSubview(toastLbl)
        NSLayoutConstraint.activate([
            containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
            containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
            containerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
            containerView.heightAnchor.constraint(equalToConstant: 50),
            toastLbl.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 0),
            toastLbl.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: 0),
            toastLbl.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 0),
            toastLbl.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: 0)
        ])
        return containerView
    }

func showToast(message: String) {
        let toast = makeToast(message: message)
        UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
            toast.alpha = 1
            toast.frame.origin.y = 50
        }, completion: { _ in
            UIView.animate(withDuration: 0.5, delay: 1.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
                toast.alpha = 0
                toast.frame.origin.y = -50
            }, completion: { _ in
                toast.removeFromSuperview()
            })
        })
    }