// // AnchorLevelTimeView.m // xplan-ios // // Created by 冯硕 on 2022/12/13. // #import "AnchorLevelTimeView.h" ///Third #import ///Tool #import "ThemeColor.h" @interface AnchorLevelTimeView () ///第一个小时 @property (nonatomic,strong) UILabel *firstHourLabel; ///第二个小时 @property (nonatomic,strong) UILabel *secondHourLabel; ///时 @property (nonatomic,strong) UILabel *hourLabel; ///第一个分钟 @property (nonatomic,strong) UILabel *firstMinuteLabel; ///第二个分钟 @property (nonatomic,strong) UILabel *secondMinuteLabel; ///分 @property (nonatomic,strong) UILabel *minuteLabel; ///第一个秒 @property (nonatomic,strong) UILabel *firstSecondLabel; ///第二个秒 @property (nonatomic,strong) UILabel *secondSecondabel; ///秒 @property (nonatomic,strong) UILabel *secondsLabel; ///定时器 @property (strong, nonatomic) dispatch_source_t timer; @end @implementation AnchorLevelTimeView - (void)dealloc { [self stopCountDown]; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initSubViews]; [self initSubViewConstraints]; } return self; } #pragma mark - Private Method - (void)initSubViews { [self addSubview:self.firstHourLabel]; [self addSubview:self.secondHourLabel]; [self addSubview:self.hourLabel]; [self addSubview:self.firstMinuteLabel]; [self addSubview:self.secondMinuteLabel]; [self addSubview:self.minuteLabel]; [self addSubview:self.firstSecondLabel]; [self addSubview:self.secondSecondabel]; [self addSubview:self.secondsLabel]; } - (void)initSubViewConstraints { [self.firstHourLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.size.mas_equalTo(CGSizeMake(24, 24)); make.top.mas_equalTo(self); make.left.mas_equalTo(self).offset(44); }]; [self.secondHourLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.size.centerY.mas_equalTo(self.firstHourLabel); make.left.mas_equalTo(self.firstHourLabel.mas_right).offset(4); }]; [self.hourLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self.firstHourLabel); make.width.mas_equalTo(20); make.left.mas_equalTo(self.secondHourLabel.mas_right); }]; [self.firstMinuteLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.size.centerY.mas_equalTo(self.firstHourLabel); make.left.mas_equalTo(self.hourLabel.mas_right).offset(0); }]; [self.secondMinuteLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.size.centerY.mas_equalTo(self.firstHourLabel); make.left.mas_equalTo(self.firstMinuteLabel.mas_right).offset(4); }]; [self.minuteLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self.firstHourLabel); make.width.mas_equalTo(20); make.left.mas_equalTo(self.secondMinuteLabel.mas_right); }]; [self.firstSecondLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.size.centerY.mas_equalTo(self.firstHourLabel); make.left.mas_equalTo(self.minuteLabel.mas_right).offset(0); }]; [self.secondSecondabel mas_makeConstraints:^(MASConstraintMaker *make) { make.size.centerY.mas_equalTo(self.firstHourLabel); make.left.mas_equalTo(self.firstSecondLabel.mas_right).offset(4); }]; [self.secondsLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.mas_equalTo(self.firstHourLabel); make.width.mas_equalTo(20); make.left.mas_equalTo(self.secondSecondabel.mas_right); }]; } // 停止倒计时 - (void)stopCountDown { if (self.timer != nil) { dispatch_source_cancel(self.timer); self.timer = nil; } } // 开始倒计时 - (void)openCountdownWithTime:(int)totalTime{ if (time <= 0) { return; } __block int time = totalTime; //倒计时时间 if (self.timer != nil) { dispatch_source_cancel(self.timer); } dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); dispatch_source_set_timer(self.timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行 __weak typeof(self) weakself = self; dispatch_source_set_event_handler(self.timer, ^{ __strong typeof(weakself) self = weakself; if(time <= 0){ //倒计时结束,关闭 dispatch_source_cancel(self.timer); dispatch_async(dispatch_get_main_queue(), ^{ self.firstHourLabel.text = @"0"; self.secondHourLabel.text = @"0"; self.firstMinuteLabel.text = @"0"; self.secondMinuteLabel.text = @"0"; self.firstSecondLabel.text = @"0"; self.secondSecondabel.text = @"0"; }); }else{ time--; dispatch_async(dispatch_get_main_queue(), ^{ int seconds = time % 60; int minutes = (time / 60) % 60; int hours = time / 3600; NSString * hour = [NSString stringWithFormat:@"%02d", hours]; NSString * minute = [NSString stringWithFormat:@"%02d", minutes]; NSString * second = [NSString stringWithFormat:@"%02d", seconds]; if (second.length > 1){ self.firstSecondLabel.text = [second substringWithRange:NSMakeRange(0, second.length -1)]; self.secondSecondabel.text = [second substringFromIndex:second.length - 1]; } if (minute.length > 1){ self.firstMinuteLabel.text = [minute substringWithRange:NSMakeRange(0, minute.length -1)]; self.secondMinuteLabel.text = [minute substringFromIndex:minute.length - 1]; } if (hour.length > 1){ self.firstHourLabel.text = [hour substringWithRange:NSMakeRange(0, hour.length -1)]; self.secondHourLabel.text = [hour substringFromIndex:hour.length - 1]; } }); } }); dispatch_resume(self.timer); } #pragma mark - Getters And Setters - (void)setNextRemaining:(int)nextRemaining { _nextRemaining = nextRemaining; if (_nextRemaining > 0) { [self openCountdownWithTime:_nextRemaining]; } } - (UILabel *)firstHourLabel { if (!_firstHourLabel) { _firstHourLabel = [[UILabel alloc] init]; _firstHourLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]; _firstHourLabel.textColor = [ThemeColor mainTextColor]; _firstHourLabel.backgroundColor = [ThemeColor colorWithHexString:@"#E8EAF3"]; _firstHourLabel.textAlignment = NSTextAlignmentCenter; _firstHourLabel.layer.masksToBounds = YES; _firstHourLabel.layer.cornerRadius = 4; _firstHourLabel.text = @"0"; } return _firstHourLabel; } - (UILabel *)secondHourLabel { if (!_secondHourLabel) { _secondHourLabel = [[UILabel alloc] init]; _secondHourLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]; _secondHourLabel.textColor = [ThemeColor mainTextColor]; _secondHourLabel.backgroundColor = [ThemeColor colorWithHexString:@"#E8EAF3"]; _secondHourLabel.textAlignment = NSTextAlignmentCenter; _secondHourLabel.layer.masksToBounds = YES; _secondHourLabel.layer.cornerRadius = 4; _secondHourLabel.text = @"0"; } return _secondHourLabel; } - (UILabel *)hourLabel { if (!_hourLabel) { _hourLabel = [[UILabel alloc] init]; _hourLabel.font = [UIFont systemFontOfSize:12]; _hourLabel.textColor = [ThemeColor mainTextColor]; _hourLabel.text = @"时"; _hourLabel.textAlignment = NSTextAlignmentCenter; } return _hourLabel; } - (UILabel *)firstMinuteLabel { if (!_firstMinuteLabel) { _firstMinuteLabel = [[UILabel alloc] init]; _firstMinuteLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]; _firstMinuteLabel.textColor = [ThemeColor mainTextColor]; _firstMinuteLabel.backgroundColor = [ThemeColor colorWithHexString:@"#E8EAF3"]; _firstMinuteLabel.textAlignment = NSTextAlignmentCenter; _firstMinuteLabel.layer.masksToBounds = YES; _firstMinuteLabel.layer.cornerRadius = 4; _firstMinuteLabel.text = @"0"; } return _firstMinuteLabel; } - (UILabel *)secondMinuteLabel { if (!_secondMinuteLabel) { _secondMinuteLabel = [[UILabel alloc] init]; _secondMinuteLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]; _secondMinuteLabel.textColor = [ThemeColor mainTextColor]; _secondMinuteLabel.backgroundColor = [ThemeColor colorWithHexString:@"#E8EAF3"]; _secondMinuteLabel.textAlignment = NSTextAlignmentCenter; _secondMinuteLabel.layer.masksToBounds = YES; _secondMinuteLabel.layer.cornerRadius = 4; _secondMinuteLabel.text = @"0"; } return _secondMinuteLabel; } - (UILabel *)minuteLabel { if (!_minuteLabel) { _minuteLabel = [[UILabel alloc] init]; _minuteLabel.font = [UIFont systemFontOfSize:12]; _minuteLabel.textColor = [ThemeColor mainTextColor]; _minuteLabel.text = @"分"; _minuteLabel.textAlignment = NSTextAlignmentCenter; } return _minuteLabel; } - (UILabel *)firstSecondLabel { if (!_firstSecondLabel) { _firstSecondLabel = [[UILabel alloc] init]; _firstSecondLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]; _firstSecondLabel.textColor = [ThemeColor mainTextColor]; _firstSecondLabel.backgroundColor = [ThemeColor colorWithHexString:@"#E8EAF3"]; _firstSecondLabel.textAlignment = NSTextAlignmentCenter; _firstSecondLabel.layer.masksToBounds = YES; _firstSecondLabel.layer.cornerRadius = 4; _firstSecondLabel.text = @"0"; } return _firstSecondLabel; } - (UILabel *)secondSecondabel { if (!_secondSecondabel) { _secondSecondabel = [[UILabel alloc] init]; _secondSecondabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium]; _secondSecondabel.textColor = [ThemeColor mainTextColor]; _secondSecondabel.backgroundColor = [ThemeColor colorWithHexString:@"#E8EAF3"]; _secondSecondabel.textAlignment = NSTextAlignmentCenter; _secondSecondabel.layer.masksToBounds = YES; _secondSecondabel.layer.cornerRadius = 4; _secondSecondabel.text = @"0"; } return _secondSecondabel; } - (UILabel *)secondsLabel { if (!_secondsLabel) { _secondsLabel = [[UILabel alloc] init]; _secondsLabel.font = [UIFont systemFontOfSize:12]; _secondsLabel.textColor = [ThemeColor mainTextColor]; _secondsLabel.text = @"秒"; _secondsLabel.textAlignment = NSTextAlignmentCenter; } return _secondsLabel; } @end