您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)ios如何實現(xiàn)倒計時器的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
此倒計時器的效果如下:
用過設置UIDatePicker的時間作為剩余時間,點擊start按鈕開始計時,UIdatePicker每隔60s修改一次剩余時間。
代碼如下:
@implementation JoyViewController
NSTimer* timer;
NSInteger leftSeconds;
- (void)viewDidLoad
{
[super viewDidLoad];
//設置使用Count Down Timer模式
self.countDonwn.datePickerMode = UIDatePickerModeCountDownTime;
}
-(IBAction)clicked:(id)sender
{
//獲取設置的剩余時間
leftSeconds = self.countDown.countDuration;
//禁用UIDatePicker控件
self.countDown.enabled = NO;
//禁用開始按鈕
[sender setEnabled] = NO;
//初始化一個字符串,用來作為警告框的內(nèi)容
NSString *message = [[NSString stringWithFormat:@"開始倒計時?您還剩下【%d】秒",leftSeconds];
//創(chuàng)建一個UIAlertView(警告框)
UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"開始倒計時?"
message:message
delegate:nil
cancelButtonTitle:@"確定"
otherButtonTitles:nil];
[alert show];
//啟用定時器,每隔60s執(zhí)行一次tickdown方法
timer = [NSTimer scheduledTimerWithTimeInterVal:60
targer:self selector:@selector(tickDown)
userInfo:nil repeates:YES];
}
- (void) tickDown
{
//將剩余時間減少60s
leftSeconds -= 60;
//修改UIDatePicker的剩余時間
self.countDown.countDownDuration = leftSeconds;
if(leftSeconds <= 0)//如果時間小于或者等于0,取消定時器
{
[timer invalidate];
//啟用定時器和按鈕
self.countDown.enabled = YES;
self.startBn.enabled = YES;
}
}
@end
感謝各位的閱讀!關(guān)于“ios如何實現(xiàn)倒計時器”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。