如何添加参数到UIViewController扩展?

时间:2019-05-30 11:51:11

标签: ios swift

我已经在UIViewController上成功创建了一个扩展,该扩展使我可以调用触发UIView进行动画处理的函数。但是,我不仅要改进普通的UIView,还需要改进扩展名,使其具有两个参数(background colorlabel text,它们是{{1 }}'popupAlert?

UIView

1 个答案:

答案 0 :(得分:4)

extension UIViewController {

    func showAlert(bgColor: UIColor, lblText: String) {
        let popupAlert = UIView(frame: CGRect(x: 0, y: -60, width: view.frame.width, height: 60))
        let popupLbl = UILabel(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 60))
        popupAlert.backgroundColor = bgColor
        popupLbl.text = lblText
        popupLbl.textColor = .green
        popupLbl.textAlignment = .center
        let window = (UIApplication.shared.delegate as! AppDelegate).window!

        popupAlert.addSubview(popupLbl)
        window.addSubview(popupAlert)

        UIView.animate(withDuration: 0.6, delay: 0, options: .curveEaseIn, animations: {
            popupAlert.transform = .init(translationX: 0, y: 60)
        }) { (_) in
            UIView.animate(withDuration: 0.6, delay: 4, options: .curveEaseIn, animations: {
                popupAlert.transform = .init(translationX: 0, y: -60)

            }, completion: { (_) in
                popupAlert.removeFromSuperview()
            })
        }
    }
}

通话

    showAlert(bgColor: .red, lblText: "testtttt")