iOS當(dāng)你需要倒計(jì)時(shí)的時(shí)候, 直接來(lái)拷貝-。-

當(dāng)項(xiàng)目中要用到倒計(jì)時(shí)的時(shí)候,直接把代碼拷過(guò)去

int secondsCountDown; //倒計(jì)時(shí)總時(shí)長(zhǎng)  
NSTimer *countDownTimer;  
UILabel *labelText;

然后具體實(shí)現(xiàn)

//創(chuàng)建UILabel 添加到當(dāng)前view  
labelText=[[UILabel alloc]initWithFrame:CGRectMake(10, 120, 120, 36)];  
[self.view addSubview:labelText];  

//設(shè)置倒計(jì)時(shí)總時(shí)長(zhǎng)  
secondsCountDown = 60;//60秒倒計(jì)時(shí)  
//開(kāi)始倒計(jì)時(shí)  
countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES]; 

//啟動(dòng)倒計(jì)時(shí)后會(huì)每秒鐘調(diào)用一次方法 timeFireMethod  

//設(shè)置倒計(jì)時(shí)顯示的時(shí)間  
labelText.text=[NSString stringWithFormat:@"%d",secondsCountDown]; 

實(shí)現(xiàn)每秒鐘執(zhí)行的方法

-(void)timeFireMethod{  
//倒計(jì)時(shí)-1  
secondsCountDown--;  
//修改倒計(jì)時(shí)標(biāo)簽現(xiàn)實(shí)內(nèi)容  
labelText.text=[NSString stringWithFormat:@"%d",secondsCountDown];  
//當(dāng)?shù)褂?jì)時(shí)到0時(shí),做需要的操作,比如驗(yàn)證碼過(guò)期不能提交  
if(secondsCountDown==0){  
    [countDownTimer invalidate];  
    [labelText removeFromSuperview];  
}  
} 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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