1.通过NSThread的performSelectorInBackground;
2.通过定时器,属于比较简单的写法;
3.通过GCD中的dispatch_source;

先说第一种:

#import "ViewController.h"@interface ViewController ()
{int _count;UILabel *_label;
}
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.//多线程读秒操作(可以加在button里控制)[self performSelectorInBackground:@selector(thread) withObject:nil];_label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];_label.backgroundColor=[UIColor orangeColor];_label.textAlignment=NSTextAlignmentCenter;_label.font=[UIFont systemFontOfSize:30];_label.text=@"60";[self.view addSubview:_label];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}
// 在异步线程中无法操作UI,如果想要操作UI必须回调主线程
- (void)thread
{for(int i=59;i>=0;i--){_count = i;// 回调主线程[self performSelectorOnMainThread:@selector(mainThread) withObject:nil waitUntilDone:YES];sleep(1);}
}// 此函数主线程执行
- (void)mainThread
{_label.text=[NSString stringWithFormat:@"%d",_count];if (_count==0) {}
}
@end

第二种:

#import "ViewController.h"@interface ViewController ()
{UIButton *btn;int timeDown; //60秒后重新获取验证码NSTimer *timer;
}
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.btn=[UIButton buttonWithType:UIButtonTypeCustom];btn.frame=CGRectMake(10, 200, 300, 50);btn.backgroundColor=[UIColor cyanColor];[btn setBackgroundImage:[UIImage imageNamed:@"bg_send_validate_code.png"] forState:UIControlStateNormal];[btn setTitle:@"获取验证码" forState:UIControlStateNormal];[btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:btn];
}- (void)btnAction{timeDown = 59;[self handleTimer];timer = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(handleTimer) userInfo:nil repeats:YES];}
-(void)handleTimer
{if(timeDown>=0){[btn setUserInteractionEnabled:NO];[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];int sec = ((timeDown%(24*3600))%3600)%60;[btn setTitle:[NSString stringWithFormat:@"(%d)重发验证码",sec] forState:UIControlStateNormal];}else{[btn setUserInteractionEnabled:YES];[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];[btn setTitle:@"重发验证码" forState:UIControlStateNormal];[timer invalidate];}timeDown = timeDown - 1;
}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end

第三种:

#import "ViewController.h"@interface ViewController ()
{UIButton *_getNumBtn;
}
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib._getNumBtn=[UIButton buttonWithType:UIButtonTypeCustom];_getNumBtn.frame=CGRectMake(10, 200, 300, 50);[_getNumBtn setBackgroundImage:[UIImage imageNamed:@"bg_send_validate_code.png"] forState:UIControlStateNormal];[_getNumBtn setTitle:@"获取验证码" forState:UIControlStateNormal];[_getNumBtn addTarget:self action:@selector(getNumBtnAction) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:_getNumBtn];
}- (void)getNumBtnAction{__block NSInteger second = 60;//全局队列    默认优先级dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//定时器模式  事件源dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, quene);//NSEC_PER_SEC是秒,*1是每秒dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), NSEC_PER_SEC * 1, 0);//设置响应dispatch源事件的block,在dispatch源指定的队列上运行dispatch_source_set_event_handler(timer, ^{//回调主线程,在主线程中操作UIdispatch_async(dispatch_get_main_queue(), ^{if (second >= 0) {[_getNumBtn setTitle:[NSString stringWithFormat:@"(%ld)重发验证码",second] forState:UIControlStateNormal];second--;}else{//这句话必须写否则会出问题dispatch_source_cancel(timer);[_getNumBtn setTitle:@"获取验证码" forState:UIControlStateNormal];}});});//启动源dispatch_resume(timer);}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end

以上写法算是比较常用的,不再坐过多说明,有问题请留言。

iOS开发-三种倒计时的写法相关推荐

  1. haproxy中acl的与或非三种規則写法

    haproxy中acl的与或非三种規則写法 当我们在haproxy里面需要使用use_backend或http-request等语句去调用定义过的acl规则时,可以跟平时写程序一样,使用与,或,非三种 ...

  2. linq拼接where语句_C# 10. LINQ 的三种查询语句写法

    C# 10. LINQ 的三种查询语句写法 前言: LINQ(语言集成查询)是 C#编程语言中的一部分.它在.NET Framework 3.5 和 C#3.0 被引入,在 System.Linq 命 ...

  3. 软件工程习题,耦合性的概念如何与软件可移植性相关联?举例支持你的论述,应用逐步求精法为下列程序开发三种不同级别的过程抽象

    软件工程习题 11.9.耦合性的概念如何与软件可移植性相关联?举例支持你的论述 11.10.应用逐步求精法为下列程序开发三种不同级别的过程抽象,开发一个支票打印程序,给出输出金额,并按支票常规要求给出 ...

  4. AutoCAD二次开发三种添加插件按钮的方法

    在上一篇关于AutoCAD的文章中,我将很多关于CAD的博客相关资源进行了说明,这一篇文章我将介绍如何在AutoCAD中的ribbon中添加相应的按钮.就是下面这种按钮: PS:在开发中我们最好使用中 ...

  5. AutoCAD二次开发三种添加插件按钮的方法之二

    上一篇相关文章主要借助了cuix配置文件来制作插件按钮,但是对于纯码农来说还是喜欢以代码来说话,今天这篇文章就来讲讲纯代码添加按钮. 开发IDE:VS2010 环境:.Net Framework4.0 ...

  6. vue开发入门篇(二)-axios POST提交数据的三种请求方式写法

    1.Content-Type: application/json let data = {"code":"1234","name":&quo ...

  7. iOS用三种途径实现一方法有多个返回值

    以前觉得这种标题有点偏向于理论,实际开发中怎么会有这种诡异的需求,但是真正遇到了这种硬需求时觉得还是有那么点价值的,理论付诸了实践在此也就做了个整理. 以我私下开发中的一处代码为例,本意是希望有这么一 ...

  8. android图片传输三方框架,Android开发三种第三方图片加载的框架

    最近在项目中用到了大量图片加载,第三方优秀框架还不错,下面介绍三款榜首的框架用法和问题,做一个记录. 现在项目使用的是Android Studio开发的,现在也没有多少人使用Eclipse了吧. 一. ...

  9. ios中三种随机数方法

    ios 有如下三种随机数方法: //第一种 srand((unsigned)time(0)); //不加这句每次产生的随机数不变 int i = rand() % 5; //第二种 srandom(t ...

最新文章

  1. QT项目添加现有文件后不能运行,MFC在类视图中自动隐藏类
  2. Python 读取和输出到txt
  3. C++ Opengl 多重纹理源码
  4. 阿里妈妈搜索广告CTR模型的“瘦身”之路
  5. Netweaver和CloudFoundry的服务器日志
  6. Table城市代码翻译
  7. 微软发布Azure Functions、Service Fabric和IoT Starter Kits新服务
  8. linux ub查看ftp安装,Linux Ubuntu 18.04 安装 FTP服务
  9. 【转】ABP源码分析一:整体项目结构及目录
  10. android设置屏幕高度和宽度设置,Android手机的屏幕宽高度和代码设置控件的宽高度...
  11. typeof()用法及JS基本类型
  12. 未找到uniwebview_unity内嵌浏览器——UniWebView插件
  13. 开源免费制造业erp erp5的优点分析
  14. Django中文文档-The Django Book
  15. 银联二维码支付之主扫、被扫、查询接口
  16. C++模拟斗地主发牌
  17. 使用rsync来实现文件同步
  18. 苹果uwb_苹果电子追踪器即将发布,我们扒了扒产品背后的 UWB 超宽频技术
  19. Java程序员怎么迈向架构师
  20. 计算机专业毕业设计致谢,计算机毕业论文致谢范文3篇

热门文章

  1. 64位 iee754_深入理解IEEE754的64位双精度
  2. python程序30行_30行Python代码,打造一个简单的微信群聊助手,简单方便
  3. JQuery点击事件会重复点击的问题
  4. 选购移动电源主要看哪些方面 如何挑选移动电源
  5. 【JVM调优】JVM原理与性能调优
  6. 虚拟机之间相互免密登录
  7. 对Java线程概念的理解
  8. MULTISIM 基本元符号
  9. C++ 字符跑酷#5 游戏制作实录
  10. java基础-原码反码补码