效果如下:

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
