IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(一)
驗(yàn)證碼倒計(jì)時(shí)按鈕的應(yīng)用是非常普遍的,該Blog就和你一起來(lái)寫一個(gè)IDCountDownButton來(lái)實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)的效果。你可以想使用普通的UIButton類型按鈕一樣,只需要設(shè)置其倒計(jì)時(shí)時(shí)長(zhǎng)(若未設(shè)置,默認(rèn)為60秒),就可以輕松的實(shí)現(xiàn)點(diǎn)擊countDownButton開始倒計(jì)時(shí),倒計(jì)時(shí)結(jié)束方可重新點(diǎn)擊。
一、實(shí)現(xiàn)效果
如圖
二、實(shí)現(xiàn)思路
1、自定義一個(gè)IDCountDownButton,重寫 beginTrackingWithTouch:withEvent: 攔截button的點(diǎn)擊事件,根據(jù)是否正在倒計(jì)時(shí)決定是否響應(yīng)并傳遞button的點(diǎn)擊事件(若倒計(jì)時(shí)正在進(jìn)行中,再次點(diǎn)擊不會(huì)重新開始倒計(jì)時(shí))
2、是用NSTimer定時(shí)器,定時(shí)改變IDCountDownButton的title
3、若倒計(jì)時(shí)結(jié)束,取消定時(shí)器并回復(fù)倒計(jì)時(shí)時(shí)長(zhǎng)(使IDCountDownButton具備再次開始倒計(jì)時(shí)的能力)
4、在IDCountDownButton銷毀時(shí),同樣取消定時(shí)器
三、實(shí)現(xiàn)步驟
1、添加相關(guān)的屬性
公有屬性(public)
@interface IDCountDownButton : UIButton /** 驗(yàn)證碼倒計(jì)時(shí)的時(shí)長(zhǎng) */ @property (nonatomic, assign) NSInteger durationOfCountDown; @end
私有屬性
@interface IDCountDownButton () /** 保存倒計(jì)時(shí)按鈕的非倒計(jì)時(shí)狀態(tài)的title */ @property (nonatomic, copy) NSString *originalTitle; /** 保存倒計(jì)時(shí)的時(shí)長(zhǎng) */ @property (nonatomic, assign) NSInteger tempDurationOfCountDown; /** 定時(shí)器對(duì)象 */ @property (nonatomic, strong) NSTimer *countDownTimer; @end
2、重寫setter
title屬性的setter
1)、私有屬性originalTitle用來(lái)暫存開始計(jì)時(shí)前button的標(biāo)題,即用戶設(shè)置的button的標(biāo)題,通常是“獲取驗(yàn)證碼”
2)、需要屏蔽計(jì)時(shí)過程中,title更新時(shí)改變originalTitle的值
- (void)setTitle:(NSString *)title forState:(UIControlState)state { [super setTitle:title forState:state]; // 倒計(jì)時(shí)過程中title的改變不更新originalTitle if (self.tempDurationOfCountDown == self.durationOfCountDown) { self.originalTitle = title; } }
durationOfCountDown屬性的setter
1)、設(shè)置tempDurationOfCountDown的值
2)、tempDurationOfCountDown的作用:倒計(jì)時(shí);與durationOfCountDown配合判斷當(dāng)前IDCountDownButton是否具備重新開始倒計(jì)時(shí)的能力
- (void)setDurationOfCountDown:(NSInteger)durationOfCountDown { _durationOfCountDown = durationOfCountDown; self.tempDurationOfCountDown = _durationOfCountDown; }
初始化
1)、設(shè)置倒計(jì)時(shí)的默認(rèn)時(shí)長(zhǎng)為60妙
2)、設(shè)置IDCountDownButton默認(rèn)的title為“獲取驗(yàn)證碼”
- (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // 設(shè)置默認(rèn)的倒計(jì)時(shí)時(shí)長(zhǎng)為60秒 self.durationOfCountDown = 60; // 設(shè)置button的默認(rèn)標(biāo)題為“獲取驗(yàn)證碼” [self setTitle:@"獲取驗(yàn)證碼" forState:UIControlStateNormal]; } return self; } - (instancetype)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { // 設(shè)置默認(rèn)的倒計(jì)時(shí)時(shí)長(zhǎng)為60秒 self.durationOfCountDown = 60; // 設(shè)置button的默認(rèn)標(biāo)題為“獲取驗(yàn)證碼” [self setTitle:@"獲取驗(yàn)證碼" forState:UIControlStateNormal]; } return self; }
攔截IDCountDownButton的點(diǎn)擊事件,判斷是否開始倒計(jì)時(shí)
1)、若tempDurationOfCountDown等于durationOfCountDown,說明未開始倒計(jì)時(shí),響應(yīng)并傳遞IDCountDownButton的點(diǎn)擊事件;否則,不響應(yīng)且不傳遞。
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event { // 若正在倒計(jì)時(shí),不響應(yīng)點(diǎn)擊事件 if (self.tempDurationOfCountDown != self.durationOfCountDown) { return NO; } // 若未開始倒計(jì)時(shí),響應(yīng)并傳遞點(diǎn)擊事件,開始倒計(jì)時(shí) [self startCountDown]; return [super beginTrackingWithTouch:touch withEvent:event]; }
倒計(jì)時(shí)
1)、創(chuàng)建定時(shí)器,開始倒計(jì)時(shí)
- (void)startCountDown { // 創(chuàng)建定時(shí)器 self.countDownTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(updateIDCountDownButtonTitle) userInfo:nil repeats:YES]; // 將定時(shí)器添加到當(dāng)前的RunLoop中(自動(dòng)開啟定時(shí)器) [[NSRunLoop currentRunLoop] addTimer:self.countDownTimer forMode:NSRunLoopCommonModes]; }
2)、更新IDCountDownButton的title為倒計(jì)時(shí)剩余的時(shí)間
- (void)updateIDCountDownButtonTitle { if (self.tempDurationOfCountDown == 0) { // 設(shè)置IDCountDownButton的title為開始倒計(jì)時(shí)前的title [self setTitle:self.originalTitle forState:UIControlStateNormal]; // 恢復(fù)IDCountDownButton開始倒計(jì)時(shí)的能力 self.tempDurationOfCountDown = self.durationOfCountDown; [self.countDownTimer invalidate]; } else { // 設(shè)置IDCountDownButton的title為當(dāng)前倒計(jì)時(shí)剩余的時(shí)間 [self setTitle:[NSString stringWithFormat:@"%zd秒", self.tempDurationOfCountDown--] forState:UIControlStateNormal]; } }
3)、移除定時(shí)器
- (void)dealloc { [self.countDownTimer invalidate]; }
使用示例
1)、添加vertificationCodeIDCountDownButton屬性
@interface ViewController () /** 驗(yàn)證碼倒計(jì)時(shí)的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)]; // 添加點(diǎn)擊事件 [self.vertificationCodeIDCountDownButton addTarget:self action:@selector(vertificationCodeIDCountDownButtonClick:) forControlEvents:UIControlEventTouchUpInside]; // 設(shè)置標(biāo)題相關(guān)屬性 [self.vertificationCodeIDCountDownButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [self.vertificationCodeIDCountDownButton setTitle:@"獲取驗(yàn)證碼" forState:UIControlStateNormal]; // 設(shè)置背景圖片 [self.vertificationCodeIDCountDownButton setBackgroundImage:[UIImage imageNamed:@"redButton"] forState:UIControlStateNormal]; // 設(shè)置倒計(jì)時(shí)時(shí)長(zhǎng) self.vertificationCodeIDCountDownButton.durationOfCountDown = 10; // 將vertificationCodeIDCountDownButton添加的控制器的view中 [self.view addSubview:self.vertificationCodeIDCountDownButton]; }
3)、實(shí)現(xiàn)點(diǎn)擊事件觸發(fā)的操作
- (void)vertificationCodeIDCountDownButtonClick:(UIButton *)button { // TODO:調(diào)用服務(wù)器接口,獲取驗(yàn)證碼 }
四、關(guān)于AppIcon
添加AppIcon時(shí)需要遵循以下規(guī)則
1)、命名,以Icon開頭(首字母大寫),跟上@2x/@3x,如圖:
2)、尺寸,必須按要求設(shè)置尺寸,如圖
3)、圖中所示的60pt對(duì)應(yīng)的圖片尺寸是
2x:120px X 120px
3x:180px X 180px
以上就是本文的全部?jī)?nèi)容,大家也可以結(jié)合第二篇IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(二)進(jìn)行學(xué)習(xí),希望對(duì)大家的學(xué)習(xí)有所幫助。
- IOS開發(fā)代碼分享之用nstimer實(shí)現(xiàn)倒計(jì)時(shí)功能
- IOS關(guān)于大型網(wǎng)站搶購(gòu)、距活動(dòng)結(jié)束,剩余時(shí)間倒計(jì)時(shí)的實(shí)現(xiàn)代碼
- ios 實(shí)現(xiàn)倒計(jì)時(shí)的兩種方式
- iOS中實(shí)現(xiàn)簡(jiǎn)單易懂秒殺倒計(jì)時(shí)/倒計(jì)時(shí)代碼
- iOS中讓多個(gè)cell上都出現(xiàn)倒計(jì)時(shí)的分析與實(shí)現(xiàn)
- iOS實(shí)現(xiàn)毫秒倒計(jì)時(shí)的方法詳解
- iOS獲取短信驗(yàn)證碼倒計(jì)時(shí)的兩種實(shí)現(xiàn)方法
- iOS啟動(dòng)頁(yè)倒計(jì)時(shí)跳過按鈕功能
- Swift實(shí)現(xiàn)iOS應(yīng)用中短信驗(yàn)證碼倒計(jì)時(shí)功能的實(shí)例分享
- iOS實(shí)現(xiàn)秒殺活動(dòng)倒計(jì)時(shí)
相關(guān)文章
iOS App開發(fā)中使用及自定義UITableViewCell的教程
這篇文章主要介紹了iOS App開發(fā)中使用及自定義UITableViewCell的教程,自定義TableViewCell文中使用Objective-C演示而非ib,需要的朋友可以參考下2016-04-04IOS 使用NSAssert()和NSParameterAssert調(diào)試程序
這篇文章主要介紹了IOS 使用NSAssert()和NSParameterAssert調(diào)試程序的相關(guān)資料,需要的朋友可以參考下2017-06-06UITextView實(shí)現(xiàn)只允許鏈接交互不允許選擇圖片的方法
這篇文章主要介紹了UITextView實(shí)現(xiàn)只允許鏈接交互不允許選擇圖片的方法,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03iOS數(shù)據(jù)持久化UserDefaults封裝器使用詳解
這篇文章主要為大家介紹了iOS數(shù)據(jù)持久化UserDefaults封裝器使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02實(shí)例解析iOS開發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用
這篇文章主要介紹了iOS開發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10