介绍
小编给大家分享一下ios如何实现倒计时,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获、下面让我们一起去了解一下吧!
<强>,方法1:使用NSTimer来实现强>
,,,,,,,主要使用的是NSTimer的scheduledTimerWithTimeInterval方法来每1秒执行一次timeFireMethod函数,timeFireMethod进行倒计时的一些操作,完成时把定时器给无效掉就好了,代码如下:,
, secondsCountDown =, 60;//60秒倒计时,=,countDownTimer [NSTimer scheduledTimerWithTimeInterval: 1,目标:self 选择器:@ selector (timeFireMethod),用户信息:nil 重复:是的),, - (void) timeFireMethod {, ,,secondsCountDown——; 如果才能(secondsCountDown==0) {, ,,(countDownTimer 无效),, ,,}, }
<>强方法2:使用GCD来实现强>
代码如下:
__block int 超时=300;,//倒计时时间, ,dispatch_queue_t queue =, dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),, ,dispatch_source_t _timer =, dispatch_source_create (DISPATCH_SOURCE_TYPE_TIMER, 0, 0,队列),, ,dispatch_source_set_timer (_timer dispatch_walltime (NULL,, 0), 1.0 * NSEC_PER_SEC, 0),,//每秒执行, ,dispatch_source_set_event_handler (_timer, ^ {, ,,如果(timeout<=0){,//倒计时结束,关闭, ,,,,dispatch_source_cancel (_timer);, ,,,,dispatch_release (_timer);, ,,,,设置(dispatch_get_main_queue (),, ^ {,//设置界面的按钮显示,根据自己需求设置, ,,,,,,........, ,,,,}),, 其他,,}{, ,,,,int minutes =, timeout /, 60岁,, ,,,,int seconds =, timeout %, 60岁,, ,,,,NSString * strTime =, (NSString stringWithFormat: @" % d分% .2d秒后重新获取验证码“,分,秒),, ,,,,设置(dispatch_get_main_queue (),, ^ {, ,,,,,,//设置界面的按钮显示,根据自己需求设置, ........, ,,,,}),, ,,,,超时,, ,,}, ,}); ,dispatch_resume (_timer);
以上是“ios如何实现倒计时”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!