| | |
| | | // |
| | | // CommonDatePickerView.swift |
| | | // WanPai |
| | | // |
| | | // Created by 无故事王国 on 2023/6/27. |
| | | // |
| | | // |
| | | // CommonDatePickerView.swift |
| | | // WanPai |
| | | // |
| | | // Created by 无故事王国 on 2023/6/27. |
| | | // |
| | | |
| | | import UIKit |
| | | import JQTools |
| | | |
| | | class CommonDatePickerView: UIView,JQNibView{ |
| | | |
| | | enum DatePickerType{ |
| | | case YMD,YM |
| | | } |
| | | |
| | | @IBOutlet weak var view_container: UIView! |
| | | @IBOutlet weak var cons_bottom: NSLayoutConstraint! |
| | | @IBOutlet weak var pickerView: UIPickerView! |
| | | private var type:DatePickerType! |
| | | |
| | | private var years = [Int]() |
| | | private var months = [1,2,3,4,5,6,7,8,9,10,11,12] |
| | | |
| | | private var days = [Int]() |
| | | private var clouse:((Int,Int,Int?)->Void)? |
| | | |
| | | override func awakeFromNib() { |
| | | super.awakeFromNib() |
| | |
| | | layoutIfNeeded() |
| | | pickerView.delegate = self |
| | | pickerView.dataSource = self |
| | | |
| | | years.append(Date().jq_nowYear()) |
| | | years.append(Date().jq_nowYear()-1) |
| | | years.append(Date().jq_nowYear()-2) |
| | | years.append(Date().jq_nowYear()-3) |
| | | } |
| | | |
| | | |
| | | static func show(){ |
| | | /// 初始化 |
| | | /// - Parameters: |
| | | /// - before: 向上N年 |
| | | /// - after: 向下N年 |
| | | /// - type: 显示状态 |
| | | static func show(before:Int = 0,after:Int = 0,type:DatePickerType = .YM,clouse:@escaping (Int,Int,Int?)->Void){ |
| | | let pickerView = CommonDatePickerView.jq_loadNibView() |
| | | pickerView.type = type |
| | | pickerView.clouse = clouse |
| | | |
| | | if before > 0{ |
| | | for i in 0...before{ |
| | | pickerView.years.append(Date().jq_nowYear() - i) |
| | | } |
| | | } |
| | | |
| | | if after > 0{ |
| | | for i in 0...after{ |
| | | pickerView.years.append(Date().jq_nowYear() + i) |
| | | } |
| | | } |
| | | pickerView.years = pickerView.years.sorted() |
| | | |
| | | pickerView.frame = screnDelegate?.window?.frame ?? .zero |
| | | screnDelegate?.window?.addSubview(pickerView) |
| | | |
| | |
| | | pickerView.alpha = 1 |
| | | pickerView.layoutIfNeeded() |
| | | } |
| | | |
| | | pickerView.pickerView.reloadAllComponents() |
| | | } |
| | | |
| | | @IBAction func hiddenAction(_ sender: UIButton) { |
| | |
| | | } |
| | | } |
| | | |
| | | @IBAction func completeAction(_ sender: UIButton) { |
| | | |
| | | |
| | | var year:Int! |
| | | var month:Int! |
| | | var day:Int? |
| | | |
| | | switch type{ |
| | | case .YMD: |
| | | year = years[pickerView.selectedRow(inComponent: 0)] |
| | | month = months[pickerView.selectedRow(inComponent: 1)] |
| | | day = pickerView.selectedRow(inComponent: 2) + 1 |
| | | case .YM: |
| | | year = years[pickerView.selectedRow(inComponent: 0)] |
| | | month = months[pickerView.selectedRow(inComponent: 1)] |
| | | case .none:break |
| | | } |
| | | |
| | | cons_bottom.constant = -(JQ_ScreenW * 0.6974) |
| | | UIView.animate(withDuration: 0.4) { |
| | | self.alpha = 0 |
| | | self.layoutIfNeeded() |
| | | } completion: { _ in |
| | | self.removeFromSuperview() |
| | | self.clouse?(year,month,day) |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | override func layoutSubviews() { |
| | | super.layoutSubviews() |
| | | view_container.jq_addCorners(corner: [.topLeft,.topRight], radius: 20) |
| | |
| | | func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { |
| | | if component == 0{ |
| | | pickerView.reloadComponent(1) |
| | | if type == .YMD{pickerView.reloadComponent(2)} |
| | | } |
| | | if component == 1{ |
| | | pickerView.reloadComponent(2) |
| | | } |
| | | } |
| | | } |
| | |
| | | extension CommonDatePickerView:UIPickerViewDataSource{ |
| | | func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { |
| | | |
| | | if component == 0{ |
| | | return years.count |
| | | } |
| | | if component == 0{return years.count |
| | | |
| | | if component == 1 && pickerView.selectedRow(inComponent: 0) == 0{ |
| | | return Date().jq_nowMonth() |
| | | }else if component == 1{ |
| | | if component == 1 && pickerView.selectedRow(inComponent: 0) == 0{ |
| | | return Date().jq_nowMonth() |
| | | } |
| | | return months.count |
| | | }else if component == 2{ |
| | | let year = years[pickerView.selectedRow(inComponent: 0)] |
| | | let month = months[pickerView.selectedRow(inComponent: 1)] |
| | | return Date.jq_getDays(year, month) |
| | | } |
| | | |
| | | return months.count |
| | | return 0 |
| | | } |
| | | |
| | | func numberOfComponents(in pickerView: UIPickerView) -> Int { |
| | | return 2 |
| | | switch type{ |
| | | case .YM:return 2 |
| | | case .YMD:return 3 |
| | | case .none:return 0 |
| | | } |
| | | } |
| | | |
| | | |
| | | func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { |
| | | return 55 |
| | |
| | | func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { |
| | | if component == 0{ |
| | | return "\(years[row])年" |
| | | }else if component == 1{ |
| | | return "\(months[row])月" |
| | | }else{ |
| | | return "\(row + 1)日" |
| | | } |
| | | return "\(months[row])月" |
| | | } |
| | | } |