【iOS 开发】时间选择器控件 UIDatePicker

iOS 时间选择器控件 UIDatePicker

1. UIDatePicker 计时器模式


// 创建 UIDatePicker
let picker1 = UIDatePicker(frame: CGRect(x: 15, y: 1050, width: 350, height: 150))

// 设置 UIDatePicker 模式
picker1.datePickerMode = UIDatePicker.Mode.countDownTimer // 计时器模式

// 设置 UIDatePicker 在计时模式下显示的时间差
picker1.countDownDuration = TimeInterval(1 * 1000)

// 设置 UIDatePicker 在计时模式下相邻时间的时间间隔
picker1.minuteInterval = 10 // 每间隔 10 分钟

picker1.addTarget(self, action: #selector(changeDatePicker), for: UIControl.Event.valueChanged)

@objc func changeDatePicker(picker: UIDatePicker) {
    
    
    print("选中的时间:\(picker.date)\(picker.countDownDuration)")
}


效果展示:
uidatepicker


2. UIDatePicker 日期时间模式


let picker4 = UIDatePicker(frame: CGRect(x: 15, y: 1350, width: 350, height: 50))

picker4.datePickerMode = UIDatePicker.Mode.dateAndTime // 日期时间模式

// 设置 UIDatePicker 地区,不设置则默认当前地区
picker4.locale = Locale.current

// 设置 UIDatePicker 时区,不设置则默认当前时区
picker4.timeZone = TimeZone.current

// 设置 UIDatePicker 选中的日期
picker4.date = Date(timeIntervalSince1970: TimeInterval(1640390400)) // 2021-12-25 08:00:00

picker4.addTarget(self, action: #selector(changeDatePicker), for: UIControl.Event.valueChanged)


效果展示:

uidatepicker2


附 Github 源码:

ViewController.swift

猜你喜欢

转载自blog.csdn.net/java_android_man/article/details/123216682