iOS 短信驗證的倒計時

定義兩個屬性//減少的秒數(shù)

@property(nonatomic,assign)NSIntegersecondsCountDown;//計時器

@property(nonatomic,strong)NSTimer*countDownTimer;

在發(fā)送驗證碼成功的時機(jī)里面

//設(shè)置不可點(diǎn)擊并開啟計時器

[selfsetRequestSmsCodeBtnCountDown];

//設(shè)置計時器并倒計時self.getButton是發(fā)送驗證碼那個button

-(void

)setRequestSmsCodeBtnCountDown{

[

self.getButtonsetEnabled:NO];//關(guān)閉交互

self.getButton.backgroundColor= [UIColorcolorWithRed:153.0/255green:153.0/255blue:153.0/255alpha:1.0];//灰色背景

self.secondsCountDown=60;//倒計時60秒

//設(shè)置并開啟計時器

self.countDownTimer= [NSTimerscheduledTimerWithTimeInterval:1target:selfselector:@selector(countDownTimeWithSeconds:)userInfo:nilrepeats:YES

];

[

_countDownTimerfire

];

}

//計時器的執(zhí)行方法

-(

void)countDownTimeWithSeconds:(NSTimer

*)timerInfo{

//判斷當(dāng)前秒數(shù)如果秒數(shù)為0,重新設(shè)置button為可點(diǎn)擊狀態(tài)

if(_secondsCountDown==0

) {

[

self.getButtonsetEnabled:YES

];

self.getButton.backgroundColor= [UIColorredColor

];

[

self.getButtonsetTitle:@"獲取驗證碼"forState:UIControlStateNormal

];

[

_countDownTimerinvalidate

];

}

else

{

//如果不為0設(shè)置button字為當(dāng)前剩余秒數(shù),并繼續(xù)減少

[

self.getButtonsetTitle:[[NSNumbernumberWithLong:_secondsCountDown]description]forState:UIControlStateNormal

];

self.secondsCountDown

--;

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容