「取得驗證碼」後的倒數計時按鈕幾乎是每個 App 的標配。本文示範一個 IDCountDownButton,用法與普通程序化 UIButton 幾乎一致:只要設定倒數秒數(不設定的話預設 60 秒),點擊後即開始倒數,倒數期間重複點擊不會重置,結束後才恢復可點擊狀態。

beginTrackingWithTouch:withEvent: 攔截點擊,依「是否正在倒數」決定要不要把事件繼續往下傳。NSTimer 每秒更新按鈕標題。dealloc 中同樣取消計時器,避免物件釋放後仍被計時器持有。對外只暴露倒數時長:
@interface IDCountDownButton : UIButton /** 驗證碼倒數的時長 */ @property (nonatomic, assign) NSInteger durationOfCountDown; @end
內部則需要三個輔助狀態:原始標題、倒數剩餘值、計時器。
@interface IDCountDownButton () /** 保存未開始計時前的標題 */ @property (nonatomic, copy) NSString *originalTitle; /** 保存倒數中的剩餘時長 */ @property (nonatomic, assign) NSInteger tempDurationOfCountDown; /** 計時器 */ @property (nonatomic, strong) NSTimer *countDownTimer; @end
覆寫 setTitle:forState:。關鍵在於:倒數過程中程式也會改寫標題,這些改動不能污染使用者設定的原始標題。利用 tempDurationOfCountDown 是否等於 durationOfCountDown 來判斷目前是否處於倒數中:
- (void)setTitle:(NSString *)title forState:(UIControlState)state {
[super setTitle:title forState:state];
// 倒數過程中標題的改變不更新 originalTitle
if (self.tempDurationOfCountDown == self.durationOfCountDown) {
self.originalTitle = title;
}
}
覆寫 setDurationOfCountDown:,同步把剩餘值設為總時長。這兩個值相等就代表「目前沒有在倒數」:
- (void)setDurationOfCountDown:(NSInteger)durationOfCountDown {
_durationOfCountDown = durationOfCountDown;
self.tempDurationOfCountDown = _durationOfCountDown;
}
初始化時給定預設值。注意同時實作 initWithFrame: 與 initWithCoder:,前者對應程式化建立,後者對應 storyboard/xib:
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.durationOfCountDown = 60; // 預設 60 秒
[self setTitle:@"獲取驗證碼" forState:UIControlStateNormal];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
self.durationOfCountDown = 60;
[self setTitle:@"獲取驗證碼" forState:UIControlStateNormal];
}
return self;
}
攔截點擊:倒數中直接回傳 NO,不響應也不往下傳;否則啟動倒數並正常回傳:
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
// 正在倒數 → 不響應
if (self.tempDurationOfCountDown != self.durationOfCountDown) {
return NO;
}
// 未開始 → 啟動倒數並傳遞事件
[self startCountDown];
return [super beginTrackingWithTouch:touch withEvent:event];
}
建立計時器。務必加入 RunLoop,否則計時器不會啟動:
- (void)startCountDown {
self.countDownTimer = [NSTimer timerWithTimeInterval:1
target:self
selector:@selector(updateIDCountDownButtonTitle)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.countDownTimer
forMode:NSRunLoopCommonModes];
}
每秒更新標題;歸零時還原標題、重置剩餘值並作廢計時器:
- (void)updateIDCountDownButtonTitle {
if (self.tempDurationOfCountDown == 0) {
[self setTitle:self.originalTitle forState:UIControlStateNormal];
self.tempDurationOfCountDown = self.durationOfCountDown;
[self.countDownTimer invalidate];
} else {
[self setTitle:[NSString stringWithFormat:@"%zd秒", self.tempDurationOfCountDown--]
forState:UIControlStateNormal];
}
}
別忘了收尾:
- (void)dealloc {
[self.countDownTimer invalidate];
}
@interface ViewController () /** 驗證碼倒數按鈕 */ @property (nonatomic, strong) IDCountDownButton *vertificationCodeIDCountDownButton; @end
- (void)viewDidLoad {
[super viewDidLoad];
self.vertificationCodeIDCountDownButton =
[[IDCountDownButton alloc] initWithFrame:CGRectMake(160, 204, 120, 44)];
[self.vertificationCodeIDCountDownButton
addTarget:self
action:@selector(vertificationCodeIDCountDownButtonClick:)
forControlEvents:UIControlEventTouchUpInside];
[self.vertificationCodeIDCountDownButton setTitleColor:[UIColor whiteColor]
forState:UIControlStateNormal];
[self.vertificationCodeIDCountDownButton setTitle:@"獲取驗證碼"
forState:UIControlStateNormal];
[self.vertificationCodeIDCountDownButton setBackgroundImage:[UIImage imageNamed:@"redButton"]
forState:UIControlStateNormal];
self.vertificationCodeIDCountDownButton.durationOfCountDown = 10;
[self.view addSubview:self.vertificationCodeIDCountDownButton];
}
- (void)vertificationCodeIDCountDownButtonClick:(UIButton *)button {
// TODO:呼叫伺服器介面取得驗證碼
}
加入 App 圖示時有幾個硬性規則:
1. 命名:以 Icon 開頭(首字母大寫),再接 @2x/@3x。

2. 尺寸:必須严格按對應的 pt 規格輸出。

3. 以 60pt 為例: