在做些活動界面或者限時驗證碼時, 經常會使用一些倒計時突出展現.
現提供兩種方案:
一.使用NSTimer定時器來倒計時
二.使用GCD來倒計時(用GCD這個寫有一個好處,跳頁不會清零, 跳頁清零會出現倒計時錯誤的)
壓縮文件截圖項目界面:
項目截圖:
一. 使用NSTimer定時器來倒計時
主要步驟:
Step1. 計算截止時間與當前時間差
Step2. 先遞減時間差 倒計時-1(總時間以秒來計算)
Step3. 給時分秒字符串通過遞減過后的秒數,重新計算數值,並輸出顯示.
獲取當天的字符串, 格式為年-月-日 時分秒:
/**
* 獲取當天的字符串
*
* @return 格式為年-月-日 時分秒
*/
- (NSString *)getCurrentTimeyyyymmdd {
NSDate *now = [NSDate date];
NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];
formatDay.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *dayStr = [formatDay stringFromDate:now];
return dayStr;
}
獲取時間差值 截止時間-當前時間:
/**
* 獲取時間差值 截止時間-當前時間
* nowDateStr : 當前時間
* deadlineStr : 截止時間
* @return 時間戳差值
*/
- (NSInteger)getDateDifferenceWithNowDateStr:(NSString*)nowDateStr deadlineStr:(NSString*)deadlineStr {
NSInteger timeDifference = 0;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yy-MM-dd HH:mm:ss"];
NSDate *nowDate = [formatter dateFromString:nowDateStr];
NSDate *deadline = [formatter dateFromString:deadlineStr];
NSTimeInterval oldTime = [nowDate timeIntervalSince1970];
NSTimeInterval newTime = [deadline timeIntervalSince1970];
timeDifference = newTime - oldTime;
return timeDifference;
}
Step1. 計算時間差值:
NSInteger secondsCountDown = [self getDateDifferenceWithNowDateStr:nowStr deadlineStr:deadlineStr];
Step2. 遞減時間差 倒計時-1(總時間以秒來計算):
secondsCountDown--;
Step3.活動倒計時:
// 啟動倒計時后會每秒鍾調用一次方法
_activeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(activeCountDownAction) userInfo:nil repeats:YES];
[_activeTimer fire];
給時分秒字符串通過遞減過后的秒數,重新計算數值,並輸出顯示:
// 重新計算 時/分/秒
NSString *str_hour = [NSString stringWithFormat:@"%02ld", secondsCountDown / 3600];
NSString *str_minute = [NSString stringWithFormat:@"%02ld", (secondsCountDown % 3600) / 60];
NSString *str_second = [NSString stringWithFormat:@"%02ld", secondsCountDown % 60];
NSString *format_time = [NSString stringWithFormat:@"%@ : %@ : %@", str_hour, str_minute, str_second];
// 修改倒計時標簽及顯示內容
self.timeLabel.text = [NSString stringWithFormat:@"使用NSTimer來實現 活動倒計時: %@", format_time];
// 當倒計時結束時做需要的操作: 比如活動到期不能提交
if(secondsCountDown <= 0) {
self.timeLabel.text = @"當前活動已結束";
[_activeTimer invalidate];
_activeTimer = nil;
return;
}
NSTimer-活動倒計時測試效果如下:
二. 使用GCD來倒計時
主要步驟:
Step1. 計算截止時間與當前時間差
Step2. 用GCD倒計時 給時分秒字符串通過遞減過后的秒數,重新計算數值,並輸出顯示, 遞減時間差 倒計時-1
Step1. 計算截止時間與當前時間差:
// 倒計時的時間 測試數據
NSString *deadlineStr = @"2017-08-19 12:00:00";
// 當前時間的時間戳
NSString *nowStr = [self getCurrentTimeyyyymmdd];
// 計算時間差值
NSInteger secondsCountDown = [self getDateDifferenceWithNowDateStr:nowStr deadlineStr:deadlineStr];
Step2.使用GCD來實現倒計時
用GCD這個寫有一個好處,跳頁不會清零 跳頁清零會出現倒計時錯誤的
活動結束等邏輯及界面處理可以按照自己需求來~
__weak __typeof(self) weakSelf = self;
if (_timer == nil) {
__block NSInteger timeout = secondsCountDown; // 倒計時時間
if (timeout!=0) {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 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){ // 當倒計時結束時做需要的操作: 關閉 活動到期不能提交
dispatch_source_cancel(_timer);
_timer = nil;
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.timeLabel.text = @"當前活動已結束";
});
} else { // 倒計時重新計算 時/分/秒
NSInteger days = (int)(timeout/(3600*24));
NSInteger hours = (int)((timeout-days*24*3600)/3600);
NSInteger minute = (int)(timeout-days*24*3600-hours*3600)/60;
NSInteger second = timeout - days*24*3600 - hours*3600 - minute*60;
NSString *strTime = [NSString stringWithFormat:@"活動倒計時 %02ld : %02ld : %02ld", hours, minute, second];
dispatch_async(dispatch_get_main_queue(), ^{
if (days == 0) {
weakSelf.timeLabel.text = strTime;
} else {
weakSelf.timeLabel.text = [NSString stringWithFormat:@"使用GCD來實現活動倒計時 %ld天 %02ld : %02ld : %02ld", days, hours, minute, second];
}
});
timeout--; // 遞減 倒計時-1(總時間以秒來計算)
}
});
dispatch_resume(_timer);
}
}
GCD-活動倒計時測試效果如下:
iOS活動倒計時的兩種實現方式
注:本文著作權歸作者,由demo大師代發,拒絕轉載,轉載需要作者授權