亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(一)

 更新時(shí)間:2016年01月10日 09:47:29   作者:世俗孤島  
這篇文章主要介紹了IOS實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能,點(diǎn)擊獲取驗(yàn)證碼,進(jìn)入時(shí)間倒計(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í)有所幫助。

相關(guān)文章

  • iOS開發(fā)定時(shí)器的三種方法分享

    iOS開發(fā)定時(shí)器的三種方法分享

    相信在大家開發(fā)過程中,常常需要在某個(gè)時(shí)間后執(zhí)行某個(gè)方法,或者是按照某個(gè)周期一直執(zhí)行某個(gè)方法。在這個(gè)時(shí)候,我們就需要用到定時(shí)器。然而,在iOS中有很多方法完成以上的任務(wù),到底有多少種方法呢?下面就通過這篇文章來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-09-09
  • iOS圖片拉伸的方法

    iOS圖片拉伸的方法

    這篇文章主要為大家詳細(xì)介紹了iOS圖片拉伸的相關(guān)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • iOS App開發(fā)中使用及自定義UITableViewCell的教程

    iOS App開發(fā)中使用及自定義UITableViewCell的教程

    這篇文章主要介紹了iOS App開發(fā)中使用及自定義UITableViewCell的教程,自定義TableViewCell文中使用Objective-C演示而非ib,需要的朋友可以參考下
    2016-04-04
  • iOS開發(fā)中Swift逃逸閉包知識(shí)

    iOS開發(fā)中Swift逃逸閉包知識(shí)

    這篇文章主要介紹了iOS開發(fā)中Swift逃逸閉包的相關(guān)知識(shí)點(diǎn)以及需要注意的地方,需要的朋友收藏下吧。
    2018-02-02
  • IOS設(shè)置按鈕為圓角的示例代碼

    IOS設(shè)置按鈕為圓角的示例代碼

    這篇文章給大家分享了IOS按鈕設(shè)置為圓角的方法,按鈕的四個(gè)角都可隨意設(shè)置為圓角,對(duì)大家開發(fā)IOS具有一定的參考借鑒價(jià)值。有需要的朋友們可以參考借鑒。
    2016-09-09
  • IOS 使用NSAssert()和NSParameterAssert調(diào)試程序

    IOS 使用NSAssert()和NSParameterAssert調(diào)試程序

    這篇文章主要介紹了IOS 使用NSAssert()和NSParameterAssert調(diào)試程序的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • UITextView實(shí)現(xiàn)只允許鏈接交互不允許選擇圖片的方法

    UITextView實(shí)現(xiàn)只允許鏈接交互不允許選擇圖片的方法

    這篇文章主要介紹了UITextView實(shí)現(xiàn)只允許鏈接交互不允許選擇圖片的方法,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-03-03
  • iOS數(shù)據(jù)持久化UserDefaults封裝器使用詳解

    iOS數(shù)據(jù)持久化UserDefaults封裝器使用詳解

    這篇文章主要為大家介紹了iOS數(shù)據(jù)持久化UserDefaults封裝器使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • 實(shí)例解析iOS開發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用

    實(shí)例解析iOS開發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用

    這篇文章主要介紹了iOS開發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-10-10
  • iOS實(shí)現(xiàn)模擬定位功能的示例代碼

    iOS實(shí)現(xiàn)模擬定位功能的示例代碼

    這篇文章主要介紹了iOS實(shí)現(xiàn)模擬定位功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11

最新評(píng)論