Swift - 使用UIDatePicker实现倒计时功能

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

下面是代码示例:
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
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()
         
         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 (type: . System )
         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()
     {
         print ( "您选择倒计时间为:\(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);
         print (leftTime)
         // 如果剩余时间小于等于0
         if (leftTime <= 0)
         {
             // 取消定时器
             timer.invalidate();
             // 启用UIDatePicker控件和按钮
             self .ctimer.enabled =  true ;
             self .btnstart.enabled =  true ;
             alertView.message =  "时间到!"
         }
     }
}

上面的代码其实还是有个小bug的。
(1) 问题描述:代码中给时间控件添加了个  ValueChanged 事件监听响应,目的是想每次选择的时间改变时都会触发打印出时间。但运行会发现,第一次拨动表盘不触发,后面再改变值才会触发。
(2) 解决办法:这个是iOS的bug,我们把设置初始时间代码
1
2
//必须为 60 的整数倍,比如设置为100,值自动变为 60
self .ctimer.countDownDuration =  NSTimeInterval (leftTime);
修改成
1
2
3
dispatch_async(dispatch_get_main_queue(), {
     self .ctimer.countDownDuration =  NSTimeInterval ( self .leftTime);
})
(如果我们不需要关心值改变事件的话,直接用原来的赋值方法即可。)
原文出自: www.hangge.com   转载请保留原文链接: http://www.hangge.com/blog/cache/detail_548.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值