IOS實現(xiàn)驗證碼倒計時功能(一)
驗證碼倒計時按鈕的應(yīng)用是非常普遍的,該Blog就和你一起來寫一個IDCountDownButton來實現(xiàn)驗證碼倒計時的效果。你可以想使用普通的UIButton類型按鈕一樣,只需要設(shè)置其倒計時時長(若未設(shè)置,默認(rèn)為60秒),就可以輕松的實現(xiàn)點擊countDownButton開始倒計時,倒計時結(jié)束方可重新點擊。
一、實現(xiàn)效果
如圖

二、實現(xiàn)思路
1、自定義一個IDCountDownButton,重寫 beginTrackingWithTouch:withEvent: 攔截button的點擊事件,根據(jù)是否正在倒計時決定是否響應(yīng)并傳遞button的點擊事件(若倒計時正在進(jìn)行中,再次點擊不會重新開始倒計時)
2、是用NSTimer定時器,定時改變IDCountDownButton的title
3、若倒計時結(jié)束,取消定時器并回復(fù)倒計時時長(使IDCountDownButton具備再次開始倒計時的能力)
4、在IDCountDownButton銷毀時,同樣取消定時器
三、實現(xiàn)步驟
1、添加相關(guān)的屬性
公有屬性(public)
@interface IDCountDownButton : UIButton /** 驗證碼倒計時的時長 */ @property (nonatomic, assign) NSInteger durationOfCountDown; @end
私有屬性
@interface IDCountDownButton () /** 保存倒計時按鈕的非倒計時狀態(tài)的title */ @property (nonatomic, copy) NSString *originalTitle; /** 保存倒計時的時長 */ @property (nonatomic, assign) NSInteger tempDurationOfCountDown; /** 定時器對象 */ @property (nonatomic, strong) NSTimer *countDownTimer; @end
2、重寫setter
title屬性的setter
1)、私有屬性originalTitle用來暫存開始計時前button的標(biāo)題,即用戶設(shè)置的button的標(biāo)題,通常是“獲取驗證碼”
2)、需要屏蔽計時過程中,title更新時改變originalTitle的值
- (void)setTitle:(NSString *)title forState:(UIControlState)state {
[super setTitle:title forState:state];
// 倒計時過程中title的改變不更新originalTitle
if (self.tempDurationOfCountDown == self.durationOfCountDown) {
self.originalTitle = title;
}
}
durationOfCountDown屬性的setter
1)、設(shè)置tempDurationOfCountDown的值
2)、tempDurationOfCountDown的作用:倒計時;與durationOfCountDown配合判斷當(dāng)前IDCountDownButton是否具備重新開始倒計時的能力
- (void)setDurationOfCountDown:(NSInteger)durationOfCountDown {
_durationOfCountDown = durationOfCountDown;
self.tempDurationOfCountDown = _durationOfCountDown;
}
初始化
1)、設(shè)置倒計時的默認(rèn)時長為60妙
2)、設(shè)置IDCountDownButton默認(rèn)的title為“獲取驗證碼”
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// 設(shè)置默認(rèn)的倒計時時長為60秒
self.durationOfCountDown = 60;
// 設(shè)置button的默認(rèn)標(biāo)題為“獲取驗證碼”
[self setTitle:@"獲取驗證碼" forState:UIControlStateNormal];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
// 設(shè)置默認(rèn)的倒計時時長為60秒
self.durationOfCountDown = 60;
// 設(shè)置button的默認(rèn)標(biāo)題為“獲取驗證碼”
[self setTitle:@"獲取驗證碼" forState:UIControlStateNormal];
}
return self;
}
攔截IDCountDownButton的點擊事件,判斷是否開始倒計時
1)、若tempDurationOfCountDown等于durationOfCountDown,說明未開始倒計時,響應(yīng)并傳遞IDCountDownButton的點擊事件;否則,不響應(yīng)且不傳遞。
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
// 若正在倒計時,不響應(yīng)點擊事件
if (self.tempDurationOfCountDown != self.durationOfCountDown) {
return NO;
}
// 若未開始倒計時,響應(yīng)并傳遞點擊事件,開始倒計時
[self startCountDown];
return [super beginTrackingWithTouch:touch withEvent:event];
}
倒計時
1)、創(chuàng)建定時器,開始倒計時
- (void)startCountDown {
// 創(chuàng)建定時器
self.countDownTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(updateIDCountDownButtonTitle) userInfo:nil repeats:YES];
// 將定時器添加到當(dāng)前的RunLoop中(自動開啟定時器)
[[NSRunLoop currentRunLoop] addTimer:self.countDownTimer forMode:NSRunLoopCommonModes];
}
2)、更新IDCountDownButton的title為倒計時剩余的時間
- (void)updateIDCountDownButtonTitle {
if (self.tempDurationOfCountDown == 0) {
// 設(shè)置IDCountDownButton的title為開始倒計時前的title
[self setTitle:self.originalTitle forState:UIControlStateNormal];
// 恢復(fù)IDCountDownButton開始倒計時的能力
self.tempDurationOfCountDown = self.durationOfCountDown;
[self.countDownTimer invalidate];
} else {
// 設(shè)置IDCountDownButton的title為當(dāng)前倒計時剩余的時間
[self setTitle:[NSString stringWithFormat:@"%zd秒", self.tempDurationOfCountDown--] forState:UIControlStateNormal];
}
}
3)、移除定時器
- (void)dealloc {
[self.countDownTimer invalidate];
}
使用示例
1)、添加vertificationCodeIDCountDownButton屬性
@interface ViewController () /** 驗證碼倒計時的button */ @property (nonatomic, strong) IDCountDownButton *vertificationCodeIDCountDownButton; @end
2)、創(chuàng)建vertificationCodeIDCountDownButton并進(jìn)行相關(guān)設(shè)置
- (void)viewDidLoad {
[super viewDidLoad];
// 創(chuàng)建vertificationCodeIDCountDownButton
self.vertificationCodeIDCountDownButton = [[IDCountDownButton alloc] initWithFrame:CGRectMake(160, 204, 120, 44)];
// 添加點擊事件
[self.vertificationCodeIDCountDownButton addTarget:self action:@selector(vertificationCodeIDCountDownButtonClick:) forControlEvents:UIControlEventTouchUpInside];
// 設(shè)置標(biāo)題相關(guān)屬性
[self.vertificationCodeIDCountDownButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.vertificationCodeIDCountDownButton setTitle:@"獲取驗證碼" forState:UIControlStateNormal];
// 設(shè)置背景圖片
[self.vertificationCodeIDCountDownButton setBackgroundImage:[UIImage imageNamed:@"redButton"] forState:UIControlStateNormal];
// 設(shè)置倒計時時長
self.vertificationCodeIDCountDownButton.durationOfCountDown = 10;
// 將vertificationCodeIDCountDownButton添加的控制器的view中
[self.view addSubview:self.vertificationCodeIDCountDownButton];
}
3)、實現(xiàn)點擊事件觸發(fā)的操作
- (void)vertificationCodeIDCountDownButtonClick:(UIButton *)button {
// TODO:調(diào)用服務(wù)器接口,獲取驗證碼
}
四、關(guān)于AppIcon
添加AppIcon時需要遵循以下規(guī)則
1)、命名,以Icon開頭(首字母大寫),跟上@2x/@3x,如圖:

2)、尺寸,必須按要求設(shè)置尺寸,如圖

3)、圖中所示的60pt對應(yīng)的圖片尺寸是
2x:120px X 120px
3x:180px X 180px
以上就是本文的全部內(nèi)容,大家也可以結(jié)合第二篇IOS實現(xiàn)驗證碼倒計時功能(二)進(jìn)行學(xué)習(xí),希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
詳解iOS應(yīng)用使用Storyboard布局時的IBOutlet與IBAction
這篇文章主要介紹了iOS應(yīng)用使用Storyboard布局時的IBOutlet與IBAction,文中還附帶講解了為什么IBOutlet屬性是weak的,需要的朋友可以參考下2016-04-04
簡單講解iOS應(yīng)用開發(fā)中的MD5加密的相關(guān)使用
這篇文章主要介紹了iOS應(yīng)用開發(fā)中的MD5加密的相關(guān)使用,示例代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12
iOS使用UICollectionView實現(xiàn)拖拽移動單元格
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)UICollectionView拖拽移動單元格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04

