// // DatePickerView.swift // Twear // // Created by yangbin on 2021/12/24. // import UIKit class ZCPickerView: UIView, UIPickerViewDelegate, UIPickerViewDataSource { typealias clickAlertClosure = (_ value: Int) -> Void var clickClosure: clickAlertClosure! private let bgView = UIView() //白色框 private lazy var titleLabel: UILabel = { let label = UILabel() label.font = RegularFont(15) label.textColor = .black label.textAlignment = .center return label }() private lazy var sureBtn: UIButton = { let button = UIButton() button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside) button.setTitle(LocString("确定"), for: .normal) button.titleLabel?.font = RegularFont(11) button.tag = 1 return button }() private lazy var cancelBtn: UIButton = { let button = UIButton() button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside) button.setTitleColor(UIColor.rgbColorFromHex(0x808080), for: .normal) button.setTitle(LocString("取消"), for: .normal) button.titleLabel?.font = RegularFont(11) button.tag = 2 return button }() var selectedRow: Int = 0 { didSet { pickerView.selectRow(self.selectedRow, inComponent: 0, animated: false) pickerView.reloadAllComponents() } } lazy var pickerView: UIPickerView = { let picker = UIPickerView() picker.delegate = self // picker.locale = .current // picker.addTarget(self, action: #selector(dateChanged(datePicker:)), for: .valueChanged) return picker }() private var suffix: String? private var titleColor: UIColor = UIColor.rgbColorFromHex(0x00993E) private var values: [Int] = [] // { // didSet { // pickerView.reloadAllComponents() // } // } init(title: String, values: [Int], suffix: String? = nil, titleColor: UIColor = UIColor.rgbColorFromHex(0x00993E), sureText: String = LocString("确定"), cancelText: String = LocString("取消")) { super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)) createView(title: title, values: values, suffix: suffix, titleColor: titleColor, sureText: sureText, cancelText: cancelText) } func createView(title: String, values: [Int], suffix: String? = nil, titleColor: UIColor, sureText: String, cancelText: String) { self.backgroundColor = UIColor.black.withAlphaComponent(0.1) bgView.backgroundColor = .white self.titleColor = titleColor self.suffix = suffix self.values = values titleLabel.text = title sureBtn.setTitle(sureText, for: .normal) sureBtn.setTitleColor(titleColor, for: .normal) cancelBtn.setTitle(cancelText, for: .normal) // pickerView.reloadAllComponents() let lineView = UIView() lineView.backgroundColor = LineColor addSubview(bgView) bgView.addSubview(titleLabel) bgView.addSubview(sureBtn) bgView.addSubview(cancelBtn) bgView.addSubview(lineView) bgView.addSubview(pickerView) bgView.snp.makeConstraints { (make) in make.left.right.bottom.equalToSuperview() make.height.equalTo(230) } cancelBtn.snp.makeConstraints { make in make.height.equalTo(40) make.width.equalTo(65) make.left.top.equalToSuperview() } sureBtn.snp.makeConstraints { make in make.height.equalTo(40) make.width.equalTo(65) make.right.top.equalToSuperview() } titleLabel.snp.makeConstraints { make in make.centerX.equalToSuperview() make.centerY.equalTo(cancelBtn) } lineView.snp.makeConstraints { make in make.left.right.equalToSuperview() make.top.equalToSuperview().offset(40) make.height.equalTo(0.5) } pickerView.snp.makeConstraints { make in make.top.equalTo(lineView) make.left.right.bottom.equalToSuperview() } bgView.layoutIfNeeded() bgView.setCorners(corners: [.topLeft, .topRight], radio: 15) } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return values.count } func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { if suffix == ":00" { return "\(String(format:"%02d",values[row])):00" } return "\(values[row])" } func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { var pickerLabel = view as? UILabel if pickerLabel == nil { pickerLabel = UILabel() pickerLabel?.font = RegularFont(20) pickerLabel?.textAlignment = .center pickerLabel?.textColor = UIColor.lightGray } if row == selectedRow { pickerLabel?.attributedText = self.pickerView(pickerView, attributedTitleForRow: selectedRow, forComponent: component) } else { pickerLabel?.text = self.pickerView(pickerView, titleForRow: row, forComponent: component) } return pickerLabel! } func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { return 30 } func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { let str = "\(values[row])" var string = NSMutableAttributedString(string: "\(values[row])") if suffix != nil { if suffix == ":00" { string = NSMutableAttributedString(string: "\(String(format:"%02d",values[row])):00") string.addAttributes([.foregroundColor: titleColor, .font: RegularFont(23)], range: NSMakeRange(0, string.length)) } else { string = NSMutableAttributedString(string: " \(values[row]) \(suffix!)") string.addAttributes([.foregroundColor: titleColor, .font: RegularFont(23)], range: NSMakeRange(0, str.length+1)) string.addAttributes([.foregroundColor: titleColor, .font: BoldFont(15)], range: NSMakeRange(str.length+1, suffix!.length+1)) } } else { string.addAttributes([.foregroundColor: titleColor, .font: RegularFont(23)], range: NSMakeRange(0, str.length)) } return string } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { selectedRow = row pickerView.reloadAllComponents() } @objc func clickBtnAction(_ sender: UIButton) { if clickClosure != nil && sender.tag == 1 { clickClosure!(values[selectedRow]) } 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: 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 } */ }