//
|
// CommonYearsPickerView.swift
|
// WanPai
|
//
|
// Created by 无故事王国 on 2024/3/5.
|
//
|
|
import UIKit
|
import JQTools
|
import QMUIKit
|
import RxSwift
|
import RxCocoa
|
|
class CommonYearsPickerView: UIView,JQNibView{
|
|
@IBOutlet weak var view_container: UIView!
|
@IBOutlet weak var cons_bottom: NSLayoutConstraint!
|
@IBOutlet weak var pickerView: UIPickerView!
|
|
private var clickClouse:((Int)->Void)!
|
private var disposeBag = DisposeBag()
|
private var years = [Int]()
|
|
override func awakeFromNib() {
|
super.awakeFromNib()
|
years = (Date().jq_nowYear()-5...Date().jq_nowYear()).map{$0}.sorted(by: {$0 > $1})
|
years.insert(0, at: 0)
|
cons_bottom.constant = -(JQ_ScreenW * 1.1)
|
pickerView.delegate = self
|
pickerView.dataSource = self
|
alpha = 0
|
layoutIfNeeded()
|
setRx()
|
}
|
|
static func show(clickClouse:@escaping (Int)->Void){
|
let studentChooseView = CommonYearsPickerView.jq_loadNibView()
|
studentChooseView.frame = sceneDelegate?.window?.frame ?? .zero
|
studentChooseView.clickClouse = clickClouse
|
sceneDelegate?.window?.addSubview(studentChooseView)
|
studentChooseView.cons_bottom.constant = 0
|
|
UIView.animate(withDuration: 0.4) {
|
studentChooseView.alpha = 1
|
studentChooseView.layoutIfNeeded()
|
}
|
}
|
|
private func setRx(){
|
|
}
|
|
@IBAction func closeAction(_ sender: UIButton) {
|
closeAction()
|
}
|
|
override func layoutSubviews() {
|
super.layoutSubviews()
|
DispatchQueue.main.asyncAfter(wallDeadline: .now()+0.1) {
|
self.view_container.jq_addCorners(corner: [.topLeft,.topRight], radius: 20)
|
}
|
}
|
|
private func closeAction(){
|
self.cons_bottom.constant = -(JQ_ScreenW * 1.1)
|
UIView.animate(withDuration: 0.4) {
|
self.alpha = 0
|
self.layoutIfNeeded()
|
} completion: { _ in
|
self.removeFromSuperview()
|
}
|
}
|
|
@IBAction func completeAction(_ sender: UIButton) {
|
let index = pickerView.selectedRow(inComponent: 0)
|
clickClouse!(years[index])
|
closeAction()
|
}
|
}
|
|
extension CommonYearsPickerView:UIPickerViewDelegate{
|
|
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
|
let label = UILabel()
|
|
if years[row] == 0{
|
label.text = "全部"
|
}else{
|
label.text = "\(years[row])年"
|
}
|
label.textColor = UIColor(hexString: "3C3C3C")
|
label.textAlignment = .center
|
label.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
|
label.bounds = CGRect(origin: .zero, size: CGSize(width: 100, height: 45))
|
return label
|
}
|
|
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
|
return 50
|
}
|
}
|
|
extension CommonYearsPickerView:UIPickerViewDataSource{
|
func numberOfComponents(in pickerView: UIPickerView) -> Int {
|
return 1
|
}
|
|
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
|
return years.count
|
}
|
}
|