APP商城需要倒計時功能,不建議使用定時器,因為定時器會有誤差問題,推薦使用GCD
首先聲明一個timer
{
dispatch_source_t _time;
}
// 倒計時時間顯示標簽
@property (nonatomic, strong) UILabel *timeLabel;
?// 設置文字顏色?
? ? UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, [UIScreen mainScreen].bounds.size.width - 50 * 2, 80)]; ? ? timeLabel.textColor = [UIColor? redColor]; ? ? timeLabel.numberOfLines = 0; ??
? [self.view addSubview:timeLabel]; ? ??
self.timeLabel = timeLabel; ?? ? ?
?? [self activeCountDownAction];
//具體倒計時操作
- (void)activeCountDownAction{
NSString *deadlineStr = @"2018-08-22 12:00:00"; //創建一個結束時間
?NSString*now? = [self getCurrentTimeyyyymmdd];//獲取當前時間
?NSIntegersencoddsCountDown = [selfgetDateDifferenceWithNowDateStr:nowdeadlineStr:deadlineStr];//時間比較
? __weak? typeof (self) weakSelf = self;
? ? if(_time==nil) {
? ? ? ? __blockNSIntegertimeout = sencoddsCountDown;// 倒計時時間
? ? ? ? if(timeout!=0) {
? ? ? ? ? ? dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
? ? ? ? ? ? _time=dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0, queue);
? ? ? ? ? ? dispatch_source_set_timer(_time, dispatch_walltime(NULL, 0), 1.0*NSEC_PER_SEC,? 0); //每秒執行
? ? ? ? ? ? dispatch_source_set_event_handler(_time, ^{
? ? ? ? ? ? ? ? if(timeout <=0){//? 當倒計時結束時做需要的操作: 關閉 活動到期不能提交
? ? ? ? ? ? ? ? ? ? dispatch_source_cancel(_time);
? ? ? ? ? ? ? ? ? ? _time=nil;
? ? ? ? ? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? ? ? ? ? ? ? weakSelf.timeLabel.text=@"當前活動已結束";
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? }else{// 倒計時重新計算 時/分/秒
? ? ? ? ? ? ? ? ? ? NSIntegerdays = (int)(timeout/(3600*24));
? ? ? ? ? ? ? ? ? ? NSIntegerhours = (int)((timeout-days*24*3600)/3600);
? ? ? ? ? ? ? ? ? ? NSIntegerminute = (int)(timeout-days*24*3600-hours*3600)/60;
? ? ? ? ? ? ? ? ? ? NSIntegersecond = timeout - days*24*3600- hours*3600- minute*60;
? ? ? ? ? ? ? ? ? ? NSString*strTime = [NSStringstringWithFormat:@"活動倒計時 %02ld : %02ld : %02ld", hours, minute, second];
? ? ? ? ? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? ? ? ? ? ? ? if(days ==0) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? weakSelf.timeLabel.text= strTime;
? ? ? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? ? ? weakSelf.timeLabel.text= [NSStringstringWithFormat:@"使用GCD來實現活動倒計時? ? ? ? ? ? %ld天 %02ld : %02ld : %02ld", days, hours, minute, second];
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? timeout--;// 遞減 倒計時-1(總時間以秒來計算)
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });
? ? ? ? ? ? dispatch_resume(_time);
? ? ? ? }
? ? }
}
//獲取本地時間
- (NSString*)getCurrentTimeyyyymmdd {
? ? NSDate *now = [NSDate date];
? ? NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];
? ? formatDay.dateFormat = @"yyyy-MM-dd HH:mm:ss";
? ? NSString*dayStr = [formatDaystringFromDate:now];
? ? returndayStr;
}
//時間比較
- (NSInteger)getDateDifferenceWithNowDateStr:(NSString*)nowDateStr deadlineStr:(NSString*)deadlineStr {
? ? NSIntegertimeDifference =0;
? ? NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
? ? [formattersetDateFormat:@"yy-MM-dd HH:mm:ss"];
? ? NSDate*nowDate = [formatterdateFromString:nowDateStr];
? ? NSDate*deadline = [formatterdateFromString:deadlineStr];
? ? NSTimeInterval oldTime = [nowDate timeIntervalSince1970];
? ? NSTimeInterval newTime = [deadline timeIntervalSince1970];
? ? timeDifference = newTime - oldTime;
? ? returntimeDifference;
}