Commit f1f7ce1e authored by pete谢兆麟's avatar pete谢兆麟

时间选择器修改

parent 7b143ab2
...@@ -34,32 +34,17 @@ class YHDatePickView: UIView { ...@@ -34,32 +34,17 @@ class YHDatePickView: UIView {
var pickerView: UIPickerView! var pickerView: UIPickerView!
var lastIsTaday: Bool = false { var lastIsTaday: Bool = false {
didSet { didSet {
if lastIsTaday { guard let type = type else { return }
guard let type = type else { return } switch type {
switch type { case .yyyymmdd:
case .yyyymmdd: pickerView.selectRow(99, inComponent: 0, animated: true)
pickerView.selectRow(0, inComponent: 0, animated: true) pickerView.selectRow(self.currentDateCom.month! - 1, inComponent: 1, animated: true)
pickerView.selectRow(self.currentDateCom.month! - 1, inComponent: 1, animated: true) pickerView.selectRow(self.currentDateCom.day! - 1, inComponent: 2, animated: true)
pickerView.selectRow(self.currentDateCom.day! - 1, inComponent: 2, animated: true) case .yyyymm:
case .yyyymm: pickerView.selectRow(99, inComponent: 0, animated: true)
pickerView.selectRow(0, inComponent: 0, animated: true) pickerView.selectRow(self.currentDateCom.month! - 1, inComponent: 1, animated: true)
pickerView.selectRow(self.currentDateCom.month! - 1, inComponent: 1, animated: true) case .yyyy:
case .yyyy: pickerView.selectRow(99, inComponent: 0, animated: true)
pickerView.selectRow(0, inComponent: 0, animated: true)
}
} else {
guard let type = type else { return }
switch type {
case .yyyymmdd:
pickerView.selectRow(100, inComponent: 0, animated: true)
pickerView.selectRow(self.currentDateCom.month! - 1, inComponent: 1, animated: true)
pickerView.selectRow(self.currentDateCom.day! - 1, inComponent: 2, animated: true)
case .yyyymm:
pickerView.selectRow(100, inComponent: 0, animated: true)
pickerView.selectRow(self.currentDateCom.month! - 1, inComponent: 1, animated: true)
case .yyyy:
pickerView.selectRow(100, inComponent: 0, animated: true)
}
} }
} }
} }
...@@ -226,23 +211,11 @@ class YHDatePickView: UIView { ...@@ -226,23 +211,11 @@ class YHDatePickView: UIView {
var date = "" var date = ""
switch type { switch type {
case .yyyymmdd: case .yyyymmdd:
if lastIsTaday { date = String(format: "%02ld%@%02ld%@%02ld", (self.currentDateCom.year!) + self.pickerView.selectedRow(inComponent: 0) - 99, Self.separator, self.pickerView.selectedRow(inComponent: 1) + 1,Self.separator, self.pickerView.selectedRow(inComponent: 2) + 1)
date = String(format: "%02ld%@%02ld%@%02ld", (self.currentDateCom.year!) - self.pickerView.selectedRow(inComponent: 0),Self.separator, self.pickerView.selectedRow(inComponent: 1) + 1,Self.separator, self.pickerView.selectedRow(inComponent: 2) + 1)
} else {
date = String(format: "%02ld%@%02ld%@%02ld", (self.currentDateCom.year!) - self.pickerView.selectedRow(inComponent: 0) + 100,Self.separator, self.pickerView.selectedRow(inComponent: 1) + 1,Self.separator, self.pickerView.selectedRow(inComponent: 2) + 1)
}
case .yyyymm: case .yyyymm:
if lastIsTaday { date = String(format: "%02ld%@%02ld", (self.currentDateCom.year!) + self.pickerView.selectedRow(inComponent: 0) - 99, Self.separator, self.pickerView.selectedRow(inComponent: 1) + 1)
date = String(format: "%02ld%@%02ld", (self.currentDateCom.year!) - self.pickerView.selectedRow(inComponent: 0) ,Self.separator, self.pickerView.selectedRow(inComponent: 1) + 1)
} else {
date = String(format: "%02ld%@%02ld", (self.currentDateCom.year!) - self.pickerView.selectedRow(inComponent: 0) + 100 ,Self.separator, self.pickerView.selectedRow(inComponent: 1) + 1)
}
case .yyyy: case .yyyy:
if lastIsTaday { date = String(format: "%02ld", (self.currentDateCom.year!) + self.pickerView.selectedRow(inComponent: 0) - 99)
date = String(format: "%02ld", (self.currentDateCom.year!) - self.pickerView.selectedRow(inComponent: 0))
} else {
date = String(format: "%02ld", (self.currentDateCom.year!) - self.pickerView.selectedRow(inComponent: 0) + 100)
}
} }
if self.backDate != nil { if self.backDate != nil {
self.backDate!(date) self.backDate!(date)
...@@ -279,7 +252,7 @@ extension YHDatePickView: UIPickerViewDelegate,UIPickerViewDataSource { ...@@ -279,7 +252,7 @@ extension YHDatePickView: UIPickerViewDelegate,UIPickerViewDataSource {
} }
return 200 return 200
} else if component == 1 { } else if component == 1 {
if self.pickerView.selectedRow(inComponent: 0) == 0 { if self.pickerView.selectedRow(inComponent: 0) == 99 {
if lastIsTaday { if lastIsTaday {
return self.currentDateCom.month! return self.currentDateCom.month!
} }
...@@ -290,7 +263,7 @@ extension YHDatePickView: UIPickerViewDelegate,UIPickerViewDataSource { ...@@ -290,7 +263,7 @@ extension YHDatePickView: UIPickerViewDelegate,UIPickerViewDataSource {
if type == .yyyy { if type == .yyyy {
return 0 return 0
} }
if self.pickerView.selectedRow(inComponent: 0) == 0 && self.pickerView.selectedRow(inComponent: 1) == self.currentDateCom.month! - 1 { if self.pickerView.selectedRow(inComponent: 0) == 99 && self.pickerView.selectedRow(inComponent: 1) == self.currentDateCom.month! - 1 {
if lastIsTaday { if lastIsTaday {
return self.currentDateCom.day! return self.currentDateCom.day!
} }
...@@ -338,10 +311,7 @@ extension YHDatePickView: UIPickerViewDelegate,UIPickerViewDataSource { ...@@ -338,10 +311,7 @@ extension YHDatePickView: UIPickerViewDelegate,UIPickerViewDataSource {
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 { if component == 0 {
if lastIsTaday { return "\((currentDateCom.year!) + row - 99)"
return "\((currentDateCom.year!) - row)"
}
return "\((currentDateCom.year!) - row + 100)"
} else if component == 1 { } else if component == 1 {
return "\(row + 1)" return "\(row + 1)"
} else { } else {
...@@ -351,11 +321,18 @@ extension YHDatePickView: UIPickerViewDelegate,UIPickerViewDataSource { ...@@ -351,11 +321,18 @@ extension YHDatePickView: UIPickerViewDelegate,UIPickerViewDataSource {
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 1, self.type == .yyyymmdd { if component == 1, self.type == .yyyymmdd {
pickerView.reloadComponent(2) pickerView.reloadComponent(1)
} }
if component == 0, lastIsTaday { if component == 0, lastIsTaday {
pickerView.reloadComponent(1) pickerView.reloadComponent(1)
if self.type == .yyyymmdd {
pickerView.reloadComponent(2)
}
}
if component == 1, lastIsTaday, self.type == .yyyymmdd {
pickerView.reloadComponent(2)
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment