使用 GCD 实现倒计时效果
效果如下:

ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController
@property (assign, nonatomic) NSInteger surplusSecond; @property (strong, nonatomic) IBOutlet UILabel *lblMessage;
@property (strong, nonatomic) IBOutlet UIButton *btnSendCAPTCHA; @end
ViewController.m
#import "ViewController.h" @interface ViewController ()
- (void)layoutUI;
- (void)countDown;
@end @implementation ViewController
#define kSurplusSecond 5 - (void)viewDidLoad {
[super viewDidLoad]; [self layoutUI];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)layoutUI {
_surplusSecond = kSurplusSecond; //剩余秒数;这里指验证码发送完,间隔多少秒才能再次点击「验证」按钮进行发送验证码 _btnSendCAPTCHA.tintColor = [UIColor darkGrayColor];
_btnSendCAPTCHA.layer.masksToBounds = YES;
_btnSendCAPTCHA.layer.cornerRadius = 10.0;
_btnSendCAPTCHA.layer.borderColor = [UIColor grayColor].CGColor;
_btnSendCAPTCHA.layer.borderWidth = 1.0;
} /**
* 倒计时
*/
- (void)countDown {
//全局并发队列
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, );
//主队列;属于串行队列
dispatch_queue_t mainQueue = dispatch_get_main_queue(); //定时循环执行事件
//dispatch_source_set_timer 方法值得一提的是最后一个参数(leeway),他告诉系统我们需要计时器触发的精准程度。所有的计时器都不会保证100%精准,这个参数用来告诉系统你希望系统保证精准的努力程度。如果你希望一个计时器每5秒触发一次,并且越准越好,那么你传递0为参数。另外,如果是一个周期性任务,比如检查email,那么你会希望每10分钟检查一次,但是不用那么精准。所以你可以传入60,告诉系统60秒的误差是可接受的。他的意义在于降低资源消耗。
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, , , globalQueue);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0.0 * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{ //计时器事件处理器
NSLog(@"Event Handler");
if (_surplusSecond <= ) {
dispatch_source_cancel(timer); //取消定时循环计时器;使得句柄被调用,即事件被执行
dispatch_async(mainQueue, ^{
_btnSendCAPTCHA.enabled = YES;
[_btnSendCAPTCHA setTitle:@"验证" forState:UIControlStateNormal]; _lblMessage.text = @"使用 GCD 实现倒计时效果";
_surplusSecond = kSurplusSecond;
});
} else {
_surplusSecond--;
dispatch_async(mainQueue, ^{
NSString *btnInfo = [NSString stringWithFormat:@"%ld秒", (long)(_surplusSecond + )];
_btnSendCAPTCHA.enabled = NO;
[_btnSendCAPTCHA setTitle:btnInfo forState:UIControlStateDisabled];
});
}
});
dispatch_source_set_cancel_handler(timer, ^{ //计时器取消处理器;调用 dispatch_source_cancel 时执行
NSLog(@"Cancel Handler");
});
dispatch_resume(timer); //恢复定时循环计时器;Dispatch Source 创建完后默认状态是挂起的,需要主动恢复,否则事件不会被传递,也不会被执行
} - (IBAction)sendCAPTCHA:(id)sender {
_lblMessage.text = [NSString stringWithFormat:@"验证码发送成功,%d秒后可重新发送", kSurplusSecond]; [self countDown];
} @end
Main.storyboard
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7706" systemVersion="14E46" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="ufC-wZ-h7g">
<objects>
<viewController id="vXZ-lx-hvc" customClass="ViewController" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="使用 GCD 实现倒计时效果" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1Kh-pV-cfz">
<rect key="frame" x="200" y="289.5" width="200" height="20.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="rFe-Xb-ZSc">
<rect key="frame" x="240" y="510" width="120" height="50"/>
<constraints>
<constraint firstAttribute="width" constant="120" id="gVH-aT-gen"/>
<constraint firstAttribute="height" constant="50" id="jJP-Vc-fpy"/>
</constraints>
<state key="normal" title="验证">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="sendCAPTCHA:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="I6T-s9-9H6"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="centerX" secondItem="rFe-Xb-ZSc" secondAttribute="centerX" id="CoE-CP-eDN"/>
<constraint firstAttribute="centerY" secondItem="1Kh-pV-cfz" secondAttribute="centerY" id="bX4-jS-0xm"/>
<constraint firstAttribute="centerX" secondItem="1Kh-pV-cfz" secondAttribute="centerX" id="mKH-Zw-Utb"/>
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="rFe-Xb-ZSc" secondAttribute="bottom" constant="40" id="y3Q-IA-qIO"/>
</constraints>
</view>
<connections>
<outlet property="btnSendCAPTCHA" destination="rFe-Xb-ZSc" id="UFG-TS-ImX"/>
<outlet property="lblMessage" destination="1Kh-pV-cfz" id="gzx-3T-euc"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
</document>
输出结果:
-- ::02.083 KMCountDown[:] Event Handler
-- ::03.087 KMCountDown[:] Event Handler
-- ::04.084 KMCountDown[:] Event Handler
-- ::05.086 KMCountDown[:] Event Handler
-- ::06.085 KMCountDown[:] Event Handler
-- ::07.085 KMCountDown[:] Event Handler
-- ::07.085 KMCountDown[:] Cancel Handler
使用 GCD 实现倒计时效果的更多相关文章
- iOS 使用GCD实现倒计时效果
在APP开发过程中,经常有需要实现倒计时效果, 比如语音验证码倒计时...代码如下: __block int timeout = 100; dispatch_queue_t queue = dispa ...
- Andorid实现点击获取验证码倒计时效果
这篇文章主要介绍了Andorid实现点击获取验证码倒计时效果,这种效果大家经常遇到,想知道如何实现的,请阅读本文 我们在开发中经常用到倒计时的功能,比如发送验证码后,倒计时60s再进行验证码的获取 ...
- jQuery Countdown Timer 倒计时效果
这个一款简单的 jQuery 倒计时插件,用于显示剩余的天数,小时,分钟和秒.倒计时功能是非常有用的一个小功能,可以告诉用户多久以后您的网站将会发布或者关闭进行维护,还可以用于举办活动的开始和停止的倒 ...
- js实现倒计时效果
<!DOCTYPE html><head><meta http-equiv="Content-Type" content="text/htm ...
- 二、JavaScript语言--JS实践--倒计时效果
主要内容:分析不同倒计时效果的计算思路及方法,掌握日期对象Date,获取时间的方法,计算时差的方法,实现不同的倒时计效果. Javascript 日期对象: Date()返回当前的日期和时间 getY ...
- javascript特效实现(4)——当前时间和倒计时效果
这个效果的实现关键是对Date对象和setTimeout的使用. 一共有三个例子,HTML结构如下,就不添加CSS样式了. <body> 当前时间:<p id="p1&qu ...
- 超实用的JavaScript代码段 --倒计时效果
现今团购网.电商网.门户网等,常使用时间记录重要的时刻,如时间显示.倒计时差.限时抢购等,本文分析不同倒计时效果的计算思路及方法,掌握日期对象Date,获取时间的方法,计算时差的方法,实现不同的倒时计 ...
- jquery网页倒计时效果,秒杀,限时抢购!
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- [jQuery编程挑战]006 生成一个倒计时效果
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
随机推荐
- Eclipse中修改包名,提交SVN时报 is out of date,怎么办?
问题:Eclipse中修改包名,提交SVN时报 is out of date,怎么办?描述: Hi,大家好! 我的问题如题,在不考虑用TortoiseSVN客户端直接删除目录这个方法的情况下,有什么方 ...
- webstorm显示行号,结构预览
1,代码结构浏览menu>view>file structure popupwindwows>tool windws >structure (alt+7)代码结构当JS代码量很 ...
- spring cloud——feign为GET请求时的对象参数传递
一.问题重现 楼主在使用feign进行声明式服务调用的时候发现,当GET请求为多参数时,为方便改用DTO对象进行参数传递.但是,在接口调用时feign会抛出一个405的请求方式错误: {"t ...
- Meclipse alt+/ 没有提示
- 阿里开源项目 druid 相关资料汇总
项目发起人访谈:http://www.iteye.com/magazines/90 github主页:https://github.com/alibaba/druid druid 项目,我想我能用很短 ...
- redis StackExchange 主备 实现 demo
网上关于redis高可用基本都是用redis-sentinel 哨兵 或者 redis cluster 集群来实现, 但是有没有更简单的方式,比如我现在就只有2个redis实例.我试验的结果是我们可用 ...
- iOS开发-观察者模式
观察者模式也被称作发布/订阅模式,观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己.观察者模式中 ...
- vscode 中使用php-cs-fixer和PHP Formatter 插件规范化PHP代码
什么是PHP-CS-Fixer? 它是php-fig组织定义的PHP代码规范,良好的代码规范可以提高代码可读性,团队沟通维护成本 使用它可以按照指定的规范格式化您的PHP代码,此工具不仅可 ...
- 2014年新一代的报表利器 Qlik Sense Desktop 初步体验
点击进入 QlikView/QlikSense 社区 交流群:432998033 Qlik Sense Desktop 的案例展示 先上几个刚刚边看边学完成的几个报表案例效果 (如果图片显示不正常, ...
- Window开启上帝模式
所谓的上帝模式就是所有功能在一个文件下,方便.快捷地操作! 01.新建一个文件夹 02.重命名文件夹 文件夹重命名为“GodMode.{ED7BA470-8E54-465E-825C-99712043 ...