效果如下:

ViewController.h
1 #import <UIKit/UIKit.h>2 3 @interface ViewController : UIViewController4 @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 510 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; //剩余秒數(shù);這里指驗(yàn)證碼發(fā)送完,間隔多少秒才能再次點(diǎn)擊「驗(yàn)證」按鈕進(jìn)行發(fā)送驗(yàn)證碼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 * 倒計(jì)時(shí)34 */35 - (void)countDown {36 //全局并發(fā)隊(duì)列37 dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);38 //主隊(duì)列;屬于串行隊(duì)列39 dispatch_queue_t mainQueue = dispatch_get_main_queue();40 41 //定時(shí)循環(huán)執(zhí)行事件42 //dispatch_source_set_timer 方法值得一提的是最后一個(gè)參數(shù)(leeway),他告訴系統(tǒng)我們需要計(jì)時(shí)器觸發(fā)的精準(zhǔn)程度。所有的計(jì)時(shí)器都不會(huì)保證100%精準(zhǔn),這個(gè)參數(shù)用來告訴系統(tǒng)你希望系統(tǒng)保證精準(zhǔn)的努力程度。如果你希望一個(gè)計(jì)時(shí)器每5秒觸發(fā)一次,并且越準(zhǔn)越好,那么你傳遞0為參數(shù)。另外,如果是一個(gè)周期性任務(wù),比如檢查email,那么你會(huì)希望每10分鐘檢查一次,但是不用那么精準(zhǔn)。所以你可以傳入60,告訴系統(tǒng)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, ^{ //計(jì)時(shí)器事件處理器46 NSLog(@"Event Handler");47 if (_surplusSecond <= 0) {48 dispatch_source_cancel(timer); //取消定時(shí)循環(huán)計(jì)時(shí)器;使得句柄被調(diào)用,即事件被執(zhí)行49 dispatch_async(mainQueue, ^{50 _btnSendCAPTCHA.enabled = YES;51 [_btnSendCAPTCHA setTitle:@"驗(yàn)證" forState:UIControlStateNormal];52 53 _lblMessage.text = @"使用 GCD 實(shí)現(xiàn)倒計(jì)時(shí)效果";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, ^{ //計(jì)時(shí)器取消處理器;調(diào)用 dispatch_source_cancel 時(shí)執(zhí)行66 NSLog(@"Cancel Handler");67 });68 dispatch_resume(timer); //恢復(fù)定時(shí)循環(huán)計(jì)時(shí)器;Dispatch Source 創(chuàng)建完后默認(rèn)狀態(tài)是掛起的,需要主動(dòng)恢復(fù),否則事件不會(huì)被傳遞,也不會(huì)被執(zhí)行69 }70 71 - (IBAction)sendCAPTCHA:(id)sender {72 _lblMessage.text = [NSString stringWithFormat:@"驗(yàn)證碼發(fā)送成功,%d秒后可重新發(fā)送", 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 實(shí)現(xiàn)倒計(jì)時(shí)效果" 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="驗(yàn)證">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>
輸出結(jié)果:
1 2015-08-31 14:40:02.083 KMCountDown[5102:103949] Event Handler2 2015-08-31 14:40:03.087 KMCountDown[5102:103949] Event Handler3 2015-08-31 14:40:04.084 KMCountDown[5102:103949] Event Handler4 2015-08-31 14:40:05.086 KMCountDown[5102:103971] Event Handler5 2015-08-31 14:40:06.085 KMCountDown[5102:103949] Event Handler6 2015-08-31 14:40:07.085 KMCountDown[5102:103949] Event Handler7 2015-08-31 14:40:07.085 KMCountDown[5102:103949] Cancel Handler
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注