#import "ViewController.h"

@interface ViewController ()
@property (nonatomic ,strong)UIButton *btn;
@property (nonatomic ,assign)NSInteger secondsCountDown;
@property (nonatomic ,strong)NSTimer *countDownTimer;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
self.btn.frame = CGRectMake(, , , );
self.btn.backgroundColor = [UIColor yellowColor];
[self.btn setTitle:@"获取验证码" forState:UIControlStateNormal]; //设置文字颜色
[self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; //添加点击事件
[self.btn addTarget:self action:@selector(startTimeGCD) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.btn];
}

1、NSTimer

//使用NSTimer实现倒计时功能
- (void)startTime {
//设置倒计时总时长
self.secondsCountDown = ;
self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(startTimeNSTimer) userInfo:nil repeats:YES];
[self.btn setTitle:[NSString stringWithFormat:@"%ld秒后重新发送",(long)self.secondsCountDown] forState:UIControlStateNormal];
} //使用NSTimer实现倒计时
- (void)startTimeNSTimer {
self.secondsCountDown -- ;
self.btn.userInteractionEnabled = NO;
[self.btn setTitle:[NSString stringWithFormat:@"%ld秒后重新发送",(long)self.secondsCountDown] forState:UIControlStateNormal];
if (self.secondsCountDown == ) {
[self.countDownTimer invalidate];
self.btn.userInteractionEnabled = YES;
[self.btn setTitle:@"重新发送验证码" forState:UIControlStateNormal];
}
}

2、GCD

//使用GCD实现倒计时
- (void)startTimeGCD {
//在block内部不可以修改外部变量,需要添加__block进行修饰
//设置倒计时总时长
__block int timeout = ;
//创建队列(全局并发队列)
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, );
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, , ,queue);
dispatch_source_set_timer(_timer,dispatch_walltime(NULL, ),1.0*NSEC_PER_SEC, ); //每秒执行
dispatch_source_set_event_handler(_timer, ^{
if(timeout <= ){
//倒计时结束,关闭
dispatch_source_cancel(_timer);
//回到主线程更新UI
dispatch_async(dispatch_get_main_queue(), ^{
//设置界面的按钮显示 根据自己需求设置
[self.btn setTitle:@"发送验证码" forState:UIControlStateNormal];
self.btn.userInteractionEnabled = YES;
});
}else{
int seconds = timeout % ;
NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
dispatch_async(dispatch_get_main_queue(), ^{
//设置界面的按钮显示 根据自己需求设置
//NSLog(@"____%@",strTime);
// [UIView beginAnimations:nil context:nil];
// [UIView setAnimationDuration:1];
[self.btn setTitle:[NSString stringWithFormat:@"%@秒后重新发送",strTime] forState:UIControlStateNormal];
// [UIView commitAnimations];
self.btn.userInteractionEnabled = NO;
});
timeout--;
}
});
dispatch_resume(_timer);
}

倒计时实现两种方法-NSTimer/GCD的更多相关文章

  1. ios实现倒计时的两种方法

    方法1:使用NSTimer来实现 主要使用的是NSTimer的scheduledTimerWithTimeInterval方法来每1秒执行一次timeFireMethod函数,timeFireMeth ...

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

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

  3. iOS活动倒计时的两种实现方式

    代码地址如下:http://www.demodashi.com/demo/11076.html 在做些活动界面或者限时验证码时, 经常会使用一些倒计时突出展现. 现提供两种方案: 一.使用NSTime ...

  4. windows下获取IP地址的两种方法

    windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...

  5. android 之 启动画面的两种方法

    现在,当我们打开任意的一个app时,其中的大部分都会显示一个启动界面,展示本公司的logo和当前的版本,有的则直接把广告放到了上面.启动画面的可以分为两种设置方式:一种是两个Activity实现,和一 ...

  6. [转载]C#读写txt文件的两种方法介绍

    C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...

  7. php如何防止图片盗用/盗链的两种方法(转)

    图片防盗链有什么用? 防止其它网站盗用你的图片,浪费你宝贵的流量.本文章向大家介绍php防止图片盗用/盗链的两种方法 Apache图片重定向方法 设置images目录不充许http访问 Apache服 ...

  8. WPF程序将DLL嵌入到EXE的两种方法

    WPF程序将DLL嵌入到EXE的两种方法 这一篇可以看作是<Visual Studio 版本转换工具WPF版开源了>的续,关于<Visual Studio 版本转换工具WPF版开源了 ...

  9. MongoDB实现分页(两种方法)

    1.插入实验数据 偷懒用下samus,100条. ; i < ; i++) { Document doc = new Document(); doc["ID"] = i; d ...

随机推荐

  1. Android -- Canvas java.lang.UnsupportedOperationException

    干货 java.lang.UnsupportedOperationException at android.view.GLES20Canvas.clipPath(GLES20Canvas.java:2 ...

  2. 【Spark】SparkStreaming-高可用-HA-Master

    SparkStreaming-高可用-HA-Master sparkstreaming master 高可用_百度搜索 Spark:Master High Availability(HA)高可用配置的 ...

  3. 3D打印机开源、免费分层软件介绍

    分层软件,就是把3D模型按照层厚设置按照Z轴方向分层,并得到G代码,供设备使用.基本上3D打印机都自带了控制软件,对于想自己开发3D打印机的朋友来说,已经有很多国外的免费或者开源的分层软件可以直接使用 ...

  4. WinForm 之 应用程序开机自启动设置方法

    一.原理 需要开机自启动的程序,需要将其启动程序的路径写到注册表中指定的文件夹下. 二.实现方式 方法1:在生成安装程序时配置: 方法2:在程序运行时动态配置. 三.在生成安装程序时配置 1.右击安装 ...

  5. VDP

    Today VMware announced a new version on their backup product vSphere Data Protection. They gave it t ...

  6. MySql【Insert Select Not Exist】判断记录再添加值的方案

    INSERT INTO content ( detail, status, beginTime, endTime) SELECT @detail, , NULL, NULL FROM DUAL WHE ...

  7. SQL Server2008为表的某列设置默认值为SQL Server函数

      例如,设置SQL Server函数GETDATE()作为默认值:

  8. JAVA GC优化入门

    为什么需要优化GC? JAVA的GC是面试必考的题目,可是在实际项目中什么时候使用GC哪?或者应该什么时候优化GC哪?有句名言:“GC优化永远是最后一项任务”. 在使用GC之前,应该考虑一下进行GC的 ...

  9. MySQL 源码系列:1:窥探篇

    1:下载源码 http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.25.tar.gz http://dev.mysql.com/downloads/m ...

  10. SpringMVC学习笔记七:SpringMVC统一异常处理

    发现有位前辈写得比我从书上看到的还详细,我就不写了,前辈的博文地址如下:  http://blog.csdn.net/eson_15/article/details/51731567