Swift - 使用UIDatePicker实现倒计时功能
如果使用UIDatePicker时将模式设置为CountDownTimer,即可让该控件作为倒计时器来使用。效果图如下:




下面是代码示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
import UIKit class ViewController : UIViewController { var ctimer: UIDatePicker ! var btnstart: UIButton ! var leftTime: Int = 180 var alertView: UIAlertView ! var timer: NSTimer ! override func viewDidLoad() { super .viewDidLoad() // Do any additional setup after loading the view, typically from a nib. ctimer = UIDatePicker (frame: CGRectMake (0.0, 120.0, 200.0, 200.0)) self .ctimer.datePickerMode = UIDatePickerMode . CountDownTimer ; //必须为 60 的整数倍,比如设置为100,值自动变为 60 self .ctimer.countDownDuration = NSTimeInterval (leftTime); ctimer.addTarget( self , action: "timerChanged" , forControlEvents: UIControlEvents . ValueChanged ) self .view.addSubview(ctimer) btnstart = UIButton .buttonWithType( UIButtonType . System ) as ! UIButton btnstart.frame = CGRect (x:100, y:400, width:100, height:100); btnstart.setTitleColor( UIColor .redColor(), forState: UIControlState . Normal ) btnstart.setTitleColor( UIColor .greenColor(), forState: UIControlState . Disabled ) btnstart.setTitle( "开始" , forState: UIControlState . Normal ) btnstart.setTitle( "倒计时中" , forState: UIControlState . Disabled ) btnstart.clipsToBounds = true ; btnstart.layer.cornerRadius = 5; btnstart.addTarget( self , action: "startClicked:" , forControlEvents: UIControlEvents . TouchUpInside ) self .view.addSubview(btnstart) } func timerChanged() { println ( "倒计时:\(self.ctimer.countDownDuration)" ) } /** *开始倒计时按钮点击 */ func startClicked(sender: UIButton ) { self .btnstart.enabled = false ; // 获取该倒计时器的剩余时间 leftTime = Int ( self .ctimer.countDownDuration); // 禁用UIDatePicker控件和按钮 self .ctimer.enabled = false ; // 创建一个UIAlertView对象(警告框),并确认,倒计时开始 alertView = UIAlertView () alertView.title = "到计时开始" alertView.message = "倒计时开始,还有 \(leftTime) 秒..." alertView.addButtonWithTitle( "确定" ) // 显示UIAlertView组件 alertView.show() // 启用计时器,控制每秒执行一次tickDown方法 timer = NSTimer .scheduledTimerWithTimeInterval( NSTimeInterval (1), target: self ,selector: Selector ( "tickDown" ), userInfo: nil ,repeats: true ) } /** *计时器每秒触发事件 **/ func tickDown() { alertView.message = "倒计时开始,还有 \(leftTime) 秒..." // 将剩余时间减少1秒 leftTime -= 1; // 修改UIDatePicker的剩余时间 self .ctimer.countDownDuration = NSTimeInterval (leftTime); println (leftTime) // 如果剩余时间小于等于0 if (leftTime <= 0) { // 取消定时器 timer.invalidate(); // 启用UIDatePicker控件和按钮 self .ctimer.enabled = true ; self .btnstart.enabled = true ; alertView.message = "时间到!" } } } |
分类:
Swift语言
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
2014-09-25 Android 出现警告Exported service does not require permission
2013-09-25 Android中的JSON详细总结