當項目中要用到倒計時的時候,直接把代碼拷過去
int secondsCountDown; //倒計時總時長
NSTimer *countDownTimer;
UILabel *labelText;
然后具體實現
//創建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];
實現每秒鐘執行的方法
-(void)timeFireMethod{
//倒計時-1
secondsCountDown--;
//修改倒計時標簽現實內容
labelText.text=[NSString stringWithFormat:@"%d",secondsCountDown];
//當倒計時到0時,做需要的操作,比如驗證碼過期不能提交
if(secondsCountDown==0){
[countDownTimer invalidate];
[labelText removeFromSuperview];
}
}
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。