// // ZCAlertView.swift // zc // // Created by wyp on 2020/11/4. // Copyright © 2020 wyp. All rights reserved. // import UIKit class ZCAlertView: UIView { typealias clickAlertClosure = (_ index: Int) -> Void var clickClosure: clickAlertClosure! let bgView = UIView() //白色框 // let bgImageView = UIImageView() let textField: UITextField = UITextField() // let countLabel = UILabel() private lazy var titleLabel: UILabel = { let label = UILabel() // label.text = title label.font = RegularFont(16) label.textColor = ZCTextColor label.textAlignment = .center label.numberOfLines = 0 return label }() private lazy var sureBtn: UIButton = { let button = UIButton() button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside) button.setColor(radio: 15, color: UIColor.rgbColorFromHex(0x3D5A80), borderColor: .clear, textColor: .white, text: LocString("确定")) button.titleLabel?.font = RegularFont(14) button.tag = 1 return button }() private lazy var cancelBtn: UIButton = { let button = UIButton() button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside) button.setColor(radio: 15, color: .white, borderColor: UIColor.rgbColorFromHex(0x999999), textColor: UIColor.rgbColorFromHex(0x999999), text: LocString("取消")) button.titleLabel?.font = RegularFont(14) button.tag = 2 return button }() init(title: String, sureText: String = LocString("确定"), cancelText: String = LocString("取消")) { super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)) createView(title: title, sureText: sureText, cancelText: cancelText) } func createView(title: String, sureText: String, cancelText: String) { // self.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT) self.backgroundColor = UIColor.black.withAlphaComponent(0.7) bgView.layer.cornerRadius = 10 bgView.clipsToBounds = true bgView.backgroundColor = .white titleLabel.text = title addSubview(bgView) bgView.addSubview(titleLabel) bgView.addSubview(sureBtn) bgView.addSubview(cancelBtn) bgView.snp.makeConstraints { (make) in make.centerX.equalToSuperview() make.centerY.equalToSuperview() make.left.equalToSuperview().offset(40) make.right.equalToSuperview().offset(-40) make.bottom.equalTo(sureBtn.snp.bottom).offset(20) // make.height.equalTo(120) } titleLabel.snp.makeConstraints { (make) in make.left.equalToSuperview().offset(25) make.right.equalToSuperview().offset(-25) make.top.equalToSuperview().offset(20) } sureBtn.snp.makeConstraints { (make) in make.centerX.equalToSuperview().multipliedBy(1.4) make.height.equalTo(30) make.width.equalTo(80) make.top.equalTo(titleLabel.snp.bottom).offset(30) } cancelBtn.snp.makeConstraints { (make) in make.centerX.equalToSuperview().multipliedBy(0.6) make.centerY.equalTo(sureBtn) make.width.equalTo(sureBtn.snp.width) make.height.equalTo(sureBtn.snp.height) } sureBtn.setTitle(sureText, for: .normal) cancelBtn.setTitle(cancelText, for: .normal) } // func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { // let textFieldText = textField.text ?? "" // let existedLength : Int = textFieldText.count // let selectedLength : Int = range.length // let replaceLength : Int = string.count // let pointLength : Int = existedLength - selectedLength + replaceLength //// print(selectedLength) // //超过16位 就不能在输入了 // if (pointLength > 16) { // return false // } else { // return true // } // } // // // @objc func clickBtnAction(_ sender: UIButton) { if (clickClosure != nil) { clickClosure!(sender.tag) } dismiss() } func show() { let wind = KeyWindow self.alpha = 0 wind.addSubview(self) UIView.animate(withDuration: 0.25, animations: { () -> Void in self.alpha = 1 }) } @objc func dismiss() { UIView.animate(withDuration: 0.25, animations: { () -> Void in self.alpha = 0 }, completion: { (finish) -> Void in if finish { self.removeFromSuperview() } }) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } /* // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func draw(_ rect: CGRect) { // Drawing code } */ }