iOS當你需要倒計時的時候, 直接來拷貝-。-

當項目中要用到倒計時的時候,直接把代碼拷過去

int secondsCountDown; //倒計時總時長  
NSTimer *countDownTimer;  
UILabel *labelText;

然后具體實現(xiàn)

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

//設置倒計時總時長  
secondsCountDown = 60;//60秒倒計時  
//開始倒計時  
countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES]; 

//啟動倒計時后會每秒鐘調用一次方法 timeFireMethod  

//設置倒計時顯示的時間  
labelText.text=[NSString stringWithFormat:@"%d",secondsCountDown]; 

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

-(void)timeFireMethod{  
//倒計時-1  
secondsCountDown--;  
//修改倒計時標簽現(xiàn)實內容  
labelText.text=[NSString stringWithFormat:@"%d",secondsCountDown];  
//當?shù)褂嫊r到0時,做需要的操作,比如驗證碼過期不能提交  
if(secondsCountDown==0){  
    [countDownTimer invalidate];  
    [labelText removeFromSuperview];  
}  
} 
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容