iOS 时间选择器控件 UIDatePicker
1. UIDatePicker 计时器模式
let picker1 = UIDatePicker(frame: CGRect(x: 15, y: 1050, width: 350, height: 150))
picker1.datePickerMode = UIDatePicker.Mode.countDownTimer
picker1.countDownDuration = TimeInterval(1 * 1000)
picker1.minuteInterval = 10
picker1.addTarget(self, action: #selector(changeDatePicker), for: UIControl.Event.valueChanged)
@objc func changeDatePicker(picker: UIDatePicker) {
print("选中的时间:\(picker.date),\(picker.countDownDuration)")
}
效果展示:
2. UIDatePicker 日期时间模式
let picker4 = UIDatePicker(frame: CGRect(x: 15, y: 1350, width: 350, height: 50))
picker4.datePickerMode = UIDatePicker.Mode.dateAndTime
picker4.locale = Locale.current
picker4.timeZone = TimeZone.current
picker4.date = Date(timeIntervalSince1970: TimeInterval(1640390400))
picker4.addTarget(self, action: #selector(changeDatePicker), for: UIControl.Event.valueChanged)
效果展示:
附 Github 源码:
ViewController.swift
|