原文:http://blog.csdn.net/kylinbl/article/details/8972261

方法1:使用NSTimer来实现

主要使用的是NSTimer的scheduledTimerWithTimeInterval方法来每1秒执行一次timeFireMethod函数,timeFireMethod进行倒计时的一些操作,完成时把timer给invalidate掉就ok了,代码如下:

  1. secondsCountDown = 60;//60秒倒计时
  2. countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
  3. -(void)timeFireMethod{
  4. secondsCountDown--;
  5. if(secondsCountDown==0){
  6. [countDownTimer invalidate];
  7. }
  8. }

方法2:使用GCD来实现

代码如下:

    1. __block int timeout=300; //倒计时时间
    2. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    3. dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    4. dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    5. dispatch_source_set_event_handler(_timer, ^{
    6. if(timeout<=0){ //倒计时结束,关闭
    7. dispatch_source_cancel(_timer);
    8. dispatch_release(_timer);
    9. dispatch_async(dispatch_get_main_queue(), ^{
    10. //设置界面的按钮显示 根据自己需求设置
    11. 。。。。。。。。
    12. });
    13. }else{
    14. int minutes = timeout / 60;
    15. int seconds = timeout % 60;
    16. NSString *strTime = [NSString stringWithFormat:@"%d分%.2d秒后重新获取验证码",minutes, seconds];
    17. dispatch_async(dispatch_get_main_queue(), ^{
    18. //设置界面的按钮显示 根据自己需求设置
    19. 。。。。。。。。
    20. });
    21. timeout--;
    22. }
    23. });
    24. dispatch_resume(_timer);

【转】 ios开发之倒计时实现的两种方法的更多相关文章

  1. ios开发——实用技术OC篇》倒计时实现的两种方法

    倒计时实现的两种方法 timeFireMethod函数,timeFireMethod进行倒计时的一些操作,完成时把timer给invalidate掉就ok了,代码如下: secondsCountDow ...

  2. iOS开发之动画编程的几种方法

    iOS开发之动画编程的几种方法 IOS中的动画总结来说有五种:UIView<block>,CAAnimation<CABasicAnimation,CATransition,CAKe ...

  3. IOS开发中数据持久化的几种方法--NSUserDefaults

    IOS开发中数据持久化的几种方法--NSUserDefaults IOS 开发中,经常会遇到需要把一些数据保存在本地的情况,那么这个时候我们有以下几种可以选择的方案: 一.使用NSUserDefaul ...

  4. Hive开发中使用变量的两种方法

    在使用hive开发数据分析代码时,经常会遇到需要改变运行参数的情况,比如select语句中对日期字段值的设定,可能不同时间想要看不同日期的数据,这就需要能动态改变日期的值.如果开发量较大.参数多的话, ...

  5. iOS开发进阶-实现多线程的3种方法

    相关文章链接: 1.多线程简介 2.实现多线程的3种方法 ......待续 前言 在多线程简介中,我已经说明过了,为了提高界面的流畅度以及用户体验.我们务必要把耗时的操作放到别的线程中去执行,千万不要 ...

  6. 使用 Eclipse 可视化插件 windowbuilder 进行Java GUI开发(插件安装的两种方法)

    对于Java GUI开发 其实最方便的方法是用插件制作,当然先了解完代码原理是最好的. eclispe安装windowbuilder有两种方式,一种是离线安装,一种是在线安装. 一.第一种在线安装: ...

  7. iOS: JS和Native交互的两种方法

    背景: UIWebView: iOS 用来展示 web 端内容的控件. 1. 核心方法: - (NSString*)stringByEvaluatingJavaScriptFromString:(NS ...

  8. iOS子线程更新UI的两种方法

    http://blog.csdn.net/libaineu2004/article/details/45368427 方法1:performSelectorOnMainThread[self perf ...

  9. iOS Swift 数组 交换元素的两种方法

    swap(&arr[fromIndexPath.row], &arr[to.row]) (arr[fromIndexPath.row],arr[to.row]) = (arr[to.r ...

随机推荐

  1. 14.8.9 Clustered and Secondary Indexes

    14.8.9 Clustered and Secondary Indexes 每个InnoDB 表有一个特殊的索引称为 clustered index 用于存储数据. 通常, clustered in ...

  2. Light OJ 1032 - Fast Bit Calculations(数位DP)

    题目大意: 一个数字把他看成二进制数字,数字里又会一些相邻的1,问从0到n至间所有相邻1的总和是多少?   分解成2进制数字,然后数位DP就行了.   ======================== ...

  3. mkfs 的使用

    使用方法: [root@localhost beinan]# mkfs -t 文件系统  存储设备 注:这里的文件系统是要指定的,比如 ext3 :reiserfs :ext2 :fat32 :msd ...

  4. Delphi安装/卸载OCX控件的方法

    delphi  安装卸载ocx 请参见 如下 http://blog.csdn.net/xt_chaoji/article/details/7027298 打开Delphi,关闭所有项目. 1.    ...

  5. yarn安装部署

    在HDFS HA(http://www.cnblogs.com/yinchengzhe/p/5140117.html)基础上进行yarn的安装. 1.配置yarn-site.xml 参数详情参考 ht ...

  6. poj 2288 tsp经典问题

    题目链接:http://poj.org/problem?id=2288 #include<cstdio> #include<cstring> #include<iostr ...

  7. lightoj 1031 区间dp

    题目链接: http://lightoj.com/volume_showproblem.php?problem=1031 #include<cstdio> #include<cstr ...

  8. 【机房系统知识小结】微软自带RDLC报表,数据汇总设计

    在做机房系统报表的时候,借鉴 八期崔成龙学长的博客< VB.NET rdlc 报表的使用>,照虎画猫的敲了一遍,但是在“汇总项”中,出现了一点小问题. 具体的设计方法在这里就不做介绍了,请 ...

  9. Python中逗号作用的实例分析

    逗号在类型转化中的使用 主要是元组的转换 例如: >>> a=11>>> b=(a)>>> b11>>> b=(a,)>& ...

  10. 洛谷 P1004 方格取数

    题目描述 设有N*N的方格图(N<=9),我们将其中的某些方格中填入正整数,而其他的方格中则放 人数字0.如下图所示(见样例): A 0 0 0 0 0 0 0 0 0 0 13 0 0 6 0 ...