前言:
點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)萝快,在項(xiàng)目中很多都會(huì)用到,尤其是現(xiàn)在應(yīng)用App當(dāng)中手機(jī)驗(yàn)證登錄著角,都會(huì)用到揪漩。本著封裝一個(gè)倒計(jì)時(shí),看到一篇文章非常好雇寇,就直接學(xué)習(xí)了氢拥,搞到自己簡(jiǎn)書中蚌铜,用于自己平常項(xiàng)目中,封裝留存嫩海。如果不同的建議冬殃,可以提出,共同探討與學(xué)習(xí)!如有侵權(quán)叁怪,告知审葬,刪除。
思想:
創(chuàng)建一個(gè)繼承于UIButton類的控件奕谭。內(nèi)部進(jìn)行樣式的編輯涣觉,加一個(gè)定時(shí)器,適時(shí)的移除血柳。當(dāng)?shù)褂?jì)時(shí)時(shí)官册,按鈕狀態(tài)是不能點(diǎn)擊的。讓倒計(jì)時(shí)每秒-1难捌,并拼接轉(zhuǎn)換成NSString膝宁。例如:剩余%ld秒。定時(shí)器根吁,數(shù)字有兩種判斷情況员淫,當(dāng)定時(shí)器數(shù)字不等于1時(shí),我們讓按鈕禁用击敌,且數(shù)字每秒-1介返,另一種情況(=1,<0),我們按鈕可以點(diǎn)擊,并移除定時(shí)器沃斤。改換按鈕內(nèi)容獲取驗(yàn)證碼圣蝎。
示例:
代碼
GGVerifyCodeViewBtn.h
#import <UIKit/UIKit.h>
@interface GGVerifyCodeViewBtn : UIButton
// 由于有些時(shí)間需求不同,特意露出方法轰枝,倒計(jì)時(shí)時(shí)間次數(shù)
- (void)timeFailBeginFrom:(NSInteger)timeCount;
@end
GGVerifyCodeViewBtn.m
#import "GGVerifyCodeViewBtn.h"
@interface GGVerifyCodeViewBtn ()
/*
* 定時(shí)器
*/
@property(strong,nonatomic) NSTimer *timer;
/*
* 定時(shí)多少秒
*/
@property(assign,nonatomic) NSInteger count;
@end
@implementation GGVerifyCodeViewBtn
#pragma mark - 初始化控件
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 配置
[self setup];
>
}
return self;
}
>
#pragma mark - 配置
- (void)setup
{
>
[self setTitle:@"獲取驗(yàn)證碼" forState:UIControlStateNormal];
self.titleLabel.font = [UIFont systemFontOfSize:10.f];
self.backgroundColor = [UIColor yellowColor];
[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
>
[self.layer setBorderColor:[UIColor redColor].CGColor];
[self.layer setBorderWidth:2];
>
self.layer.cornerRadius = 3.0f;
self.layer.masksToBounds = YES;
>
}
#pragma mark - 添加定時(shí)器
- (void)timeFailBeginFrom:(NSInteger)timeCount
{
self.count = timeCount;
self.enabled = NO;
// 加1個(gè)定時(shí)器
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeDown) userInfo: nil repeats:YES];
>
>
}
#pragma mark - 定時(shí)器事件
- (void)timeDown
{
if (self.count != 1){
>
self.count -=1;
self.enabled = NO;
[self setTitle:[NSString stringWithFormat:@"剩余%ld秒", self.count] forState:UIControlStateNormal];
>
} else {
>
self.enabled = YES;
[self setTitle:@"獲取驗(yàn)證碼" forState:UIControlStateNormal];
[self.timer invalidate];
}
>
}
用法:
ViewController.m
#import "ViewController.h"
#import "GGVerifyCodeViewBtn.h"
@interface ViewController ()
/*
* 獲取驗(yàn)證碼按鈕
*/
@property (nonatomic, strong) GGVerifyCodeViewBtn *codeBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
>
// 獲取驗(yàn)證碼按鈕
GGVerifyCodeViewBtn *codeBtn = [[GGVerifyCodeViewBtn alloc]init];
// 設(shè)置frame 這里我是按照自己需求來(lái)
codeBtn.frame = CGRectMake(100, 100, 80, 30);
[self.view addSubview:codeBtn];
[codeBtn addTarget:self action:@selector(codeBtnVerification) forControlEvents:UIControlEventTouchUpInside];
self.codeBtn = codeBtn;
}
#pragma mark - 獲取驗(yàn)證碼點(diǎn)擊事件
- (void)codeBtnVerification {
>
/*
>
調(diào)用短信驗(yàn)證碼接口
用戶輸入的驗(yàn)證碼數(shù)字傳給server捅彻,判斷請(qǐng)求結(jié)果作不同的邏輯處理,根據(jù)自己家的產(chǎn)品大大需求來(lái)即可....
*/
>
/*
if (請(qǐng)求成功且匹配成功驗(yàn)證碼數(shù)字){
[self.codeBtn timeFailBeginFrom:60]; // 倒計(jì)時(shí)60s
} else {
[self.codeBtn timeFailBeginFrom:1]; // 處理請(qǐng)求成功但是匹配不成功的情況鞍陨,并不需要執(zhí)行倒計(jì)時(shí)功能
}
*/
>
[self.codeBtn timeFailBeginFrom:60];
}
@end
Demo地址:https://github.com/RenZhengYang/GGGetValidationCode