倒计时实现两种方法-NSTimer/GCD
#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的更多相关文章
- ios实现倒计时的两种方法
方法1:使用NSTimer来实现 主要使用的是NSTimer的scheduledTimerWithTimeInterval方法来每1秒执行一次timeFireMethod函数,timeFireMeth ...
- ios开发——实用技术OC篇》倒计时实现的两种方法
倒计时实现的两种方法 timeFireMethod函数,timeFireMethod进行倒计时的一些操作,完成时把timer给invalidate掉就ok了,代码如下: secondsCountDow ...
- iOS活动倒计时的两种实现方式
代码地址如下:http://www.demodashi.com/demo/11076.html 在做些活动界面或者限时验证码时, 经常会使用一些倒计时突出展现. 现提供两种方案: 一.使用NSTime ...
- windows下获取IP地址的两种方法
windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...
- android 之 启动画面的两种方法
现在,当我们打开任意的一个app时,其中的大部分都会显示一个启动界面,展示本公司的logo和当前的版本,有的则直接把广告放到了上面.启动画面的可以分为两种设置方式:一种是两个Activity实现,和一 ...
- [转载]C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...
- php如何防止图片盗用/盗链的两种方法(转)
图片防盗链有什么用? 防止其它网站盗用你的图片,浪费你宝贵的流量.本文章向大家介绍php防止图片盗用/盗链的两种方法 Apache图片重定向方法 设置images目录不充许http访问 Apache服 ...
- WPF程序将DLL嵌入到EXE的两种方法
WPF程序将DLL嵌入到EXE的两种方法 这一篇可以看作是<Visual Studio 版本转换工具WPF版开源了>的续,关于<Visual Studio 版本转换工具WPF版开源了 ...
- MongoDB实现分页(两种方法)
1.插入实验数据 偷懒用下samus,100条. ; i < ; i++) { Document doc = new Document(); doc["ID"] = i; d ...
随机推荐
- jquery.cookie 使用方法
一个轻量级的cookie 插件,可以读取.写入.删除 cookie. jquery.cookie.js 的配置 首先包含jQuery的库文件,在后面包含 jquery.cookie.js 的库文件. ...
- 【Python】无须numpy,利用map函数与zip(*)函数对数组转置(转)
http://blog.csdn.net/yongh701/article/details/50283689 在Python的numpy中,对类似array=[[1,2,3],[4,5,6],[7,8 ...
- 如何在原生工程中引入Cordova工程-for iOS 【转】
http://blog.csdn.net/e20914053/article/details/50170487 如今混合开发方兴未艾,有的项目可能一开始是原生开发的,后期需要加入混合开发,如将Cord ...
- Introduction to the visual formatting model
原文:https://www.w3.org/TR/CSS2/visuren.html#block-formatting --------------------------------------- ...
- Linux反编译
转自:http://bbs.pediy.com/showthread.php?threadid=11315 一个简单的linux crackme的逆向前言 最不喜欢的就是写破解教程,酒后一时冲动 ...
- disconf-自动注入属性变化
disconf-自动注入属性变化 disconf官网:http://disconf.readthedocs.io/zh_CN/latest/ 自动注入属性:http://disconf.readthe ...
- (文档)Shader.Find (在编译时,只包含那些使用中的shader或位置在"Resources"文件夹中shader)
Shader.Find 查找 static function Find (name : string) : Shader Description描述 Finds a shader with the g ...
- pycharm下设置自己的模板
在File---settings---File and Code Templates---Python script 脚本里添加: #!usr/bin/env python #-*- coding:u ...
- 【Python】使用geopy由经纬度找地理信息
from geopy.geocoders import Nominatim geolocator = Nominatim() location = geolocator.reverse("3 ...
- 构造并发送Beacon帧以伪造任意WiFi热点
请想象一下这样的情景:你可以任意伪造很多个WiFi热点, 这个技术只能在linux上使用,而且对无线网卡也有一定的挑剔,具体的下面会讲- 阶段一:基本原理 首先需要搞清楚的是,手机.电脑等支持WiFi ...