方法一:使用NSTimer來實現(比較適用於發送短信驗證碼倒計時)
主要是利用NSTimer的scheduledTimerWithTimeInterval方法來每秒執行一次changeTime方法
//創建一個Timer,每秒執行一次changeTime:方法
NSTimer * timer =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTime:) userInfo:nil repeats:YES];
//changeTime
-(void)changeTime:(NSTimer*)timer
{
//點擊獲取驗證碼的btn
UIButton * button = (UIButton*)[self.view viewWithTag:99];
if (count == 0) {
//完成後invalidate掉
[timer invalidate];
//59s倒計時
count = 59;
[button setTitle:@"重新獲取" forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
button.alpha = 1;
}
else{
[button setTitle:[NSString stringWithFormat:@"%d s",count] forState:UIControlStateNormal];
count--;
}
}
方法二:使用GCD來實現(比較使用於商家做某種活動的倒計時)
.h文件中定義一個Timer來控制時間
//倒計時Timer
dispatch_source_t _timer;
.m文件中實現:
//創建一個時間戳
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
//時間戳的格式
[dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
//將相同時間戳格式的NSString類型的數據轉換成NSDate類型的數據
NSDate *endDate = [dateFormatter dateFromString:_EndTime];
NSDate *startDate = [dateFormatter dateFromString:_StartTime];
NSDate *currentDate = [dateFormatter dateFromString:_CurrentTime];
//計算服務器當前時間距離活動結束的時間戳
NSTimeInterval timeInterval =[endDate timeIntervalSinceDate:currentDate];
//計算服務器當前時間與活動開始時間的時間戳
NSTimeInterval StartToNow = [currentDate timeIntervalSinceDate:startDate];
//倒計時時間
__block int timeout = timeInterval;
__block int StartTimeout = StartToNow;
if (timeout!=0) {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 並行隊列
// 並行隊列可以同時處理多個任務,在不得以的情況下可以用dispatch_queue_create創建,但一般我們都要用系統預定義的並行隊列,即全局隊列(Global // Concurrent Dispatch Queues)。目前系統預定義了四個不同運行優先級的全局隊列,我們可以通過dispatch_get_global_queue來獲取它們。
//dispatch_get_global_queue第一個參數是隊列的優先級,分別對應四個全局隊列:
//DISPATCH_QUEUE_PRIORITY_HIGH
//DISPATCH_QUEUE_PRIORITY_DEFAULT
//DISPATCH_QUEUE_PRIORITY_LOW
//DISPATCH_QUEUE_PRIORITY_BACKGROUND
//dispatch_get_global_queue中第二個參數目前系統保留,請設置為0即可。
//每秒執行
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0);
dispatch_source_set_event_handler(_timer, ^{
//倒計時結束,關閉
if(timeout<=0 || StartTimeout <=0){
dispatch_source_cancel(_timer);
_timer = nil;
//在隊列中運行任務
//你可以隨時向一個隊列中添加一個新任務,只需要調用一下dispatch_async即可:
dispatch_async(dispatch_get_main_queue(), ^{
//可以根據自己需求設計需要顯示的內容及展現格式、風格等
dayLabel.text = @"0天";
hourLabel.text = @"00 :";
minLabel.text = @"00 :";
secLabel.text = @"00";
label.text = @"搶購結束!!!";
});
}else{
label.text = @"搶購剩余時間:";
int days = (int)(timeout/(3600*24));
if (days==0) {
dayLabel.text = @"";
}
int hours = (int)((timeout-days*24*3600)/3600);
int minute = (int)(timeout-days*24*3600-hours*3600)/60;
int second = timeout-days*24*3600-hours*3600-minute*60;
dispatch_async(dispatch_get_main_queue(), ^{
if (days==0) {
dayLabel.text = @"0天";
}else{
dayLabel.text = [NSString stringWithFormat:@"%d天",days];
}
if (hours<10) {
hourLabel.text = [NSString stringWithFormat:@"0%d :",hours];
}else{
hourLabel.text = [NSString stringWithFormat:@"%d :",hours];
}
if (minute<10) {
minLabel.text = [NSString stringWithFormat:@"0%d :",minute];
}else{
minLabel.text = [NSString stringWithFormat:@"%d :",minute];
}
if (second<10) {
secLabel.text = [NSString stringWithFormat:@"0%d",second];
}else{
secLabel.text = [NSString stringWithFormat:@"%d",second];
}
});
timeout--;
}
});
dispatch_resume(_timer);
}