这是一种方法。这是一个快速破解,因为您没有可以设置自动启用它的属性。
UIDatePicker没有有任何UIControl.Event,我们可以为其添加一个目标操作对,以便在我们滚动时通知我们值的连续变化。 date 属性仅在完全停止时更新。所以...
基本上我使用Timer,并从子视图中提取信息,检测日期何时结束,确定月份应该增加还是减少,然后我在月份调用scrollToRow(:) UITableView。
class VC: UIViewController {
var picker: UIDatePicker!
var currentMonth: Month? {
didSet {
if let oldValue = oldValue, oldValue != currentMonth {
print(currentMonth!)
}
}
}
var currentDay: Int? {
didSet {
if let oldValue = oldValue, oldValue != currentDay {
print(currentDay!)
if [28, 30, 31].contains(oldValue) && currentDay == 1 {
print("Incr the month!")
self.incrMonth()
} else if oldValue == 1 && [28, 30, 31].contains(currentDay) {
print("Decr the month!")
self.decrMonth()
}
}
}
}
func incrMonth() {
let cell = self.monthTableView.visibleCells.last!
let indexPath = self.monthTableView.indexPath(for: cell)!
let newIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
self.monthTableView.scrollToRow(at: newIndexPath, at: .bottom, animated: true)
}
func decrMonth() {
let cell = self.monthTableView.visibleCells.last!
let indexPath = self.monthTableView.indexPath(for: cell)!
let newIndexPath = IndexPath(row: indexPath.row - 1, section: 0)
self.monthTableView.scrollToRow(at: newIndexPath, at: .bottom, animated: true)
}
var monthTableView: UITableView!
var dayTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
picker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 200))
picker.preferredDatePickerStyle = .wheels
picker.datePickerMode = .date
picker.center = view.center
view.addSubview(picker)
// The wheels style picker consists of 3 dials.
// The month, day, year dials are each represented by not one, but two `UITableViews`.
// One for the top half of a dial and one for the bottom half, which is interesting.
// The last visible cell of a top half tableview is always the highlighted one.
monthTableView = self.picker.subviews[0].subviews[0].subviews[0].subviews[0].subviews[0] as! UITableView
dayTableView = self.picker.subviews[0].subviews[0].subviews[1].subviews[0].subviews[0] as! UITableView
let timer = Timer(timeInterval: 0.01, repeats: true) { _ in
let monthLabel = self.monthTableView.visibleCells.last!.subviews[0].subviews[0].subviews[0] as! UILabel
let dayLabel = self.dayTableView.visibleCells.last!.subviews[0].subviews[0].subviews[0] as! UILabel
let day = Int(dayLabel.text!)!
self.currentDay = day
self.currentMonth = Month(rawValue: monthLabel.text!)
}
// So the timer won't be blocked by UI interactions
RunLoop.current.add(timer, forMode: RunLoop.Mode.common)
}
}
enum Month: String {
case January
case February
case March
case April
case May
case June
case July
case August
case September
case October
case November
case December
func number() -> Int {
switch self {
case .January: return 1
case .February: return 2
case .March: return 3
case .April: return 4
case .May: return 5
case .June: return 6
case .July: return 7
case .August: return 8
case .September: return 9
case .October: return 10
case .November: return 11
case .December: return 12
}
}
}