APP商城需要倒計(jì)時(shí)功能纠修,不建議使用定時(shí)器,因?yàn)槎〞r(shí)器會(huì)有誤差問(wèn)題您没,推薦使用GCD
首先聲明一個(gè)timer
{
dispatch_source_t _time;
}
// 倒計(jì)時(shí)時(shí)間顯示標(biāo)簽
@property (nonatomic, strong) UILabel *timeLabel;
?// 設(shè)置文字顏色?
? ? 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];
//具體倒計(jì)時(shí)操作
- (void)activeCountDownAction{
NSString *deadlineStr = @"2018-08-22 12:00:00"; //創(chuàng)建一個(gè)結(jié)束時(shí)間
?NSString*now? = [self getCurrentTimeyyyymmdd];//獲取當(dāng)前時(shí)間
?NSIntegersencoddsCountDown = [selfgetDateDifferenceWithNowDateStr:nowdeadlineStr:deadlineStr];//時(shí)間比較
? __weak? typeof (self) weakSelf = self;
? ? if(_time==nil) {
? ? ? ? __blockNSIntegertimeout = sencoddsCountDown;// 倒計(jì)時(shí)時(shí)間
? ? ? ? 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); //每秒執(zhí)行
? ? ? ? ? ? dispatch_source_set_event_handler(_time, ^{
? ? ? ? ? ? ? ? if(timeout <=0){//? 當(dāng)?shù)褂?jì)時(shí)結(jié)束時(shí)做需要的操作: 關(guān)閉 活動(dòng)到期不能提交
? ? ? ? ? ? ? ? ? ? dispatch_source_cancel(_time);
? ? ? ? ? ? ? ? ? ? _time=nil;
? ? ? ? ? ? ? ? ? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? ? ? ? ? ? ? ? ? weakSelf.timeLabel.text=@"當(dāng)前活動(dòng)已結(jié)束";
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? }else{// 倒計(jì)時(shí)重新計(jì)算 時(shí)/分/秒
? ? ? ? ? ? ? ? ? ? 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:@"活動(dòng)倒計(jì)時(shí) %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來(lái)實(shí)現(xiàn)活動(dòng)倒計(jì)時(shí)? ? ? ? ? ? %ld天 %02ld : %02ld : %02ld", days, hours, minute, second];
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? timeout--;// 遞減 倒計(jì)時(shí)-1(總時(shí)間以秒來(lái)計(jì)算)
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });
? ? ? ? ? ? dispatch_resume(_time);
? ? ? ? }
? ? }
}
//獲取本地時(shí)間
- (NSString*)getCurrentTimeyyyymmdd {
? ? NSDate *now = [NSDate date];
? ? NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];
? ? formatDay.dateFormat = @"yyyy-MM-dd HH:mm:ss";
? ? NSString*dayStr = [formatDaystringFromDate:now];
? ? returndayStr;
}
//時(shí)間比較
- (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;
}