使用 GCD 实现倒计时效果

效果如下:

ViewController.h

1 #import <UIKit/UIKit.h>
2
3 @interface ViewController : UIViewController
4 @property (assign, nonatomic) NSInteger surplusSecond;
5
6 @property (strong, nonatomic) IBOutlet UILabel *lblMessage;
7 @property (strong, nonatomic) IBOutlet UIButton *btnSendCAPTCHA;
8
9 @end

ViewController.m

 1 #import "ViewController.h"
 2
 3 @interface ViewController ()
 4 - (void)layoutUI;
 5 - (void)countDown;
 6 @end
 7
 8 @implementation ViewController
 9 #define kSurplusSecond 5
10
11 - (void)viewDidLoad {
12     [super viewDidLoad];
13
14     [self layoutUI];
15 }
16
17 - (void)didReceiveMemoryWarning {
18     [super didReceiveMemoryWarning];
19     // Dispose of any resources that can be recreated.
20 }
21
22 - (void)layoutUI {
23     _surplusSecond = kSurplusSecond; //剩余秒数;这里指验证码发送完,间隔多少秒才能再次点击「验证」按钮进行发送验证码
24
25     _btnSendCAPTCHA.tintColor = [UIColor darkGrayColor];
26     _btnSendCAPTCHA.layer.masksToBounds = YES;
27     _btnSendCAPTCHA.layer.cornerRadius = 10.0;
28     _btnSendCAPTCHA.layer.borderColor = [UIColor grayColor].CGColor;
29     _btnSendCAPTCHA.layer.borderWidth = 1.0;
30 }
31
32 /**
33  *  倒计时
34  */
35 - (void)countDown {
36     //全局并发队列
37     dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
38     //主队列;属于串行队列
39     dispatch_queue_t mainQueue = dispatch_get_main_queue();
40
41     //定时循环执行事件
42     //dispatch_source_set_timer 方法值得一提的是最后一个参数(leeway),他告诉系统我们需要计时器触发的精准程度。所有的计时器都不会保证100%精准,这个参数用来告诉系统你希望系统保证精准的努力程度。如果你希望一个计时器每5秒触发一次,并且越准越好,那么你传递0为参数。另外,如果是一个周期性任务,比如检查email,那么你会希望每10分钟检查一次,但是不用那么精准。所以你可以传入60,告诉系统60秒的误差是可接受的。他的意义在于降低资源消耗。
43     dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, globalQueue);
44     dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0.0 * NSEC_PER_SEC);
45     dispatch_source_set_event_handler(timer, ^{ //计时器事件处理器
46         NSLog(@"Event Handler");
47         if (_surplusSecond <= 0) {
48             dispatch_source_cancel(timer); //取消定时循环计时器;使得句柄被调用,即事件被执行
49             dispatch_async(mainQueue, ^{
50                 _btnSendCAPTCHA.enabled = YES;
51                 [_btnSendCAPTCHA setTitle:@"验证" forState:UIControlStateNormal];
52
53                 _lblMessage.text = @"使用 GCD 实现倒计时效果";
54                 _surplusSecond = kSurplusSecond;
55             });
56         } else {
57             _surplusSecond--;
58             dispatch_async(mainQueue, ^{
59                 NSString *btnInfo = [NSString stringWithFormat:@"%ld秒", (long)(_surplusSecond + 1)];
60                 _btnSendCAPTCHA.enabled = NO;
61                 [_btnSendCAPTCHA setTitle:btnInfo forState:UIControlStateDisabled];
62             });
63         }
64     });
65     dispatch_source_set_cancel_handler(timer, ^{ //计时器取消处理器;调用 dispatch_source_cancel 时执行
66         NSLog(@"Cancel Handler");
67     });
68     dispatch_resume(timer);  //恢复定时循环计时器;Dispatch Source 创建完后默认状态是挂起的,需要主动恢复,否则事件不会被传递,也不会被执行
69 }
70
71 - (IBAction)sendCAPTCHA:(id)sender {
72     _lblMessage.text = [NSString stringWithFormat:@"验证码发送成功,%d秒后可重新发送", kSurplusSecond];
73
74     [self countDown];
75 }
76
77 @end

Main.storyboard

 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 2 <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">
 3     <dependencies>
 4         <deployment identifier="iOS"/>
 5         <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
 6     </dependencies>
 7     <scenes>
 8         <!--View Controller-->
 9         <scene sceneID="ufC-wZ-h7g">
10             <objects>
11                 <viewController id="vXZ-lx-hvc" customClass="ViewController" sceneMemberID="viewController">
12                     <layoutGuides>
13                         <viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
14                         <viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
15                     </layoutGuides>
16                     <view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
17                         <rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
18                         <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
19                         <subviews>
20                             <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">
21                                 <rect key="frame" x="200" y="289.5" width="200" height="20.5"/>
22                                 <fontDescription key="fontDescription" type="system" pointSize="17"/>
23                                 <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
24                                 <nil key="highlightedColor"/>
25                             </label>
26                             <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="rFe-Xb-ZSc">
27                                 <rect key="frame" x="240" y="510" width="120" height="50"/>
28                                 <constraints>
29                                     <constraint firstAttribute="width" constant="120" id="gVH-aT-gen"/>
30                                     <constraint firstAttribute="height" constant="50" id="jJP-Vc-fpy"/>
31                                 </constraints>
32                                 <state key="normal" title="验证">
33                                     <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
34                                 </state>
35                                 <connections>
36                                     <action selector="sendCAPTCHA:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="I6T-s9-9H6"/>
37                                 </connections>
38                             </button>
39                         </subviews>
40                         <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
41                         <constraints>
42                             <constraint firstAttribute="centerX" secondItem="rFe-Xb-ZSc" secondAttribute="centerX" id="CoE-CP-eDN"/>
43                             <constraint firstAttribute="centerY" secondItem="1Kh-pV-cfz" secondAttribute="centerY" id="bX4-jS-0xm"/>
44                             <constraint firstAttribute="centerX" secondItem="1Kh-pV-cfz" secondAttribute="centerX" id="mKH-Zw-Utb"/>
45                             <constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="rFe-Xb-ZSc" secondAttribute="bottom" constant="40" id="y3Q-IA-qIO"/>
46                         </constraints>
47                     </view>
48                     <connections>
49                         <outlet property="btnSendCAPTCHA" destination="rFe-Xb-ZSc" id="UFG-TS-ImX"/>
50                         <outlet property="lblMessage" destination="1Kh-pV-cfz" id="gzx-3T-euc"/>
51                     </connections>
52                 </viewController>
53                 <placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
54             </objects>
55         </scene>
56     </scenes>
57 </document>

输出结果:

1 2015-08-31 14:40:02.083 KMCountDown[5102:103949] Event Handler
2 2015-08-31 14:40:03.087 KMCountDown[5102:103949] Event Handler
3 2015-08-31 14:40:04.084 KMCountDown[5102:103949] Event Handler
4 2015-08-31 14:40:05.086 KMCountDown[5102:103971] Event Handler
5 2015-08-31 14:40:06.085 KMCountDown[5102:103949] Event Handler
6 2015-08-31 14:40:07.085 KMCountDown[5102:103949] Event Handler
7 2015-08-31 14:40:07.085 KMCountDown[5102:103949] Cancel Handler
时间: 2024-11-08 23:56:32

使用 GCD 实现倒计时效果的相关文章

javascript特效实现(4)——当前时间和倒计时效果

这个效果的实现关键是对Date对象和setTimeout的使用. 一共有三个例子,HTML结构如下,就不添加CSS样式了. <body> 当前时间:<p id="p1"></p> 高考倒计时:<p id="p2"></p> 限时抢购:<p id="p3"></p> </body> 主要体会javascript的实现 window.onload=func

[jQuery编程挑战]006 生成一个倒计时效果

<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8"/> <title>生成一个倒计时效果</title> <style type="text/css"> body{ margin:0; padding:0; background: orange; width: 100%; height: 10

Andorid实现点击获取验证码倒计时效果

这篇文章主要介绍了Andorid实现点击获取验证码倒计时效果,这种效果大家经常遇到,想知道如何实现的,请阅读本文 我们在开发中经常用到倒计时的功能,比如发送验证码后,倒计时60s再进行验证码的获取,为了方便以后使用,这里做个记录,讲讲倒计时器的实现. 1.先进行倒计时工具类的封装 1 public class CountDownTimerUtils extends CountDownTimer { 2 private TextView mTextView; 3 4 /** 5 * @param

bobojQuery实现倒计时效果

使用jQuery实现倒计时效果,这个实例是在页面上显示剩余几天几小时几分几秒的效果. 在头部引用最新的jQuery.js文件: <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="jQuery.js" type="text/javascript"></scri

网页上的倒计时效果

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>jquery数字倒计时代码</title&

js实现倒计时效果

<!DOCTYPE html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>倒计时效果</title><link rel="stylesheet" href="css/demo.css" /></head> <body&g

使用setTimeout()实现倒计时效果代码实例

使用setTimeout()实现倒计时效果代码实例:大多数情况下实现倒计时效果是使用setInterval()函数,因为此函数可以每隔指定的实现就执行一次指定函数,而setTimeout()函数只能够执行一次,不过也是可以实现倒计时效果的,下面就通过代码实例介绍一下如何利用setTimeout()函数实现倒计时效果.代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8">

倒计时效果显示

<!DOCTYPE html><html><head>    <title>倒计时效果显示</title>    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">    <script type="text/javascript" src="https://code.jquer

canvas绘图详解-03-绚丽的倒计时效果

制作一个上面这种倒计时效果,只截图了三秒 此效果源码github连接:https://github.com/wufangfang0614/fanggit/tree/master/canvas/%E5%80%92%E8%AE%A1%E6%97%B6%E6%97%B6%E9%92%9F 首先分析这个得有一定的数学知识和物理知识(看这个课程的时候,心想要是让我自己想明白这个过程我可弄不明白.)