随笔 - 400,  文章 - 0,  评论 - 7,  阅读 - 21万

 

复制代码
@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]]; } }
复制代码

 

posted on   懂事长qingzZ  阅读(172)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示