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

【iOS 开发】时间选择器控件 UIDatePicker,第1张

目录 iOS 时间选择器控件 UIDatePicker1. UIDatePicker 计时器模式2. UIDatePicker 日期时间模式附 Github 源码:

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)")
}


效果展示:


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)


效果展示:


附 Github 源码:

ViewController.swift

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/990418.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-21
下一篇2022-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存