OC 添加GCD 定时器

@property (nonatomic,strong) dispatch_source_t timer;

///不是在dealloc 修改为空
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (self.timer){
        dispatch_source_cancel(self.timer);
        self.timer = nil;
    }
}


///使用
[self startCountDownAction];

///开启倒计时
- (void)startCountDownAction{
    NSLog(@"[self getDiffTime] = %@", [self getDiffTime]);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    self.timer = _timer;
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1*NSEC_PER_SEC, 0); //每秒执行
    @WeakObj(self);
    dispatch_source_set_event_handler(_timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"[self getDiffTime] = %@", [self getDiffTime]);
            @StrongObj(self);
            self.countDownLabel.attributedText = [self getDiffTime];
        });
    });
    dispatch_resume(_timer);
}



///9时22分32秒 8毫秒
///时间差
- (NSMutableAttributedString *)getDiffTime{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *nowDate = [NSDate date];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:nowDate];
    ///今天0点
    NSDate *startDate = [calendar dateFromComponents:components];
    ///下一天的0点
    NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0];
    
///俩日期差值
double diffTime = [nowDate secondsEarlierThan:endDate]; NSDate *detailDate = [NSDate dateWithTimeIntervalSince1970:diffTime]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; //实例化一个NSDateFormatter对象 [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]]; NSMutableAttributedString *dikou = [ToolUtil markString:@"距今日活动结束仅剩" color:colorFromRGB(0x434446) font:[UIFont regularFontOfSize:11]]; [dateFormatter setDateFormat:@" H "]; NSString *diffHour = [dateFormatter stringFromDate: detailDate]; [dikou appendAttributedString:[ToolUtil markString:diffHour color:colorFromRGB(0x1F1F20) font:[UIFont boldSystemFontOfSize:12]]]; [dikou appendAttributedString:[ToolUtil markString:@"" color:colorFromRGB(0x434446) font:[UIFont boldSystemFontOfSize:11]]]; [dateFormatter setDateFormat:@" m "]; NSString *diffMinute = [dateFormatter stringFromDate: detailDate]; [dikou appendAttributedString:[ToolUtil markString:diffMinute color:colorFromRGB(0x1F1F20) font:[UIFont boldSystemFontOfSize:12]]]; [dikou appendAttributedString:[ToolUtil markString:@"" color:colorFromRGB(0x434446) font:[UIFont boldSystemFontOfSize:11]]]; [dateFormatter setDateFormat:@" s "]; NSString *diffSecond = [dateFormatter stringFromDate: detailDate]; [dikou appendAttributedString:[ToolUtil markString:diffSecond color:colorFromRGB(0x1F1F20) font:[UIFont boldSystemFontOfSize:12]]]; [dikou appendAttributedString:[ToolUtil markString:@"" color:colorFromRGB(0x434446) font:[UIFont boldSystemFontOfSize:11]]]; return dikou; }

#pragma mark- ============ 富文本 ============== + (NSMutableAttributedString *)markString:(NSString *)str color:(UIColor *)color font:(UIFont *)font { NSMutableAttributedString *attributedString_M = [[NSMutableAttributedString alloc] initWithString:str ]; if ([self isNull:str]) { return attributedString_M; } if (![self isNull:color]) { [attributedString_M addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, str.length)]; } if (![self isNull:font]) { [attributedString_M addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, str.length)]; } return attributedString_M; }



#pragma mark- 万能判空 + (BOOL)isNull:(id)obj { if ([obj isKindOfClass:[NSString class]]) { return obj == nil || [obj isEqual:[NSNull null]] || [obj isEqualToString:@"<null>"] || [self isEmpty:obj] || [obj isEqualToString:@"null"] || [obj isEqualToString:@"(null)"]; } else { return obj == nil || [obj isEqual:[NSNull null]]; } }
原文地址:https://www.cnblogs.com/qingzZ/p/13914345.html