@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]]; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现