【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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值