// // KWTimeEndView.m // westkissMob // // Created by iOS on 2022/9/6. // #import "KWTimeEndView.h" @interface KWTimeEndView () @property (nonatomic, strong) UILabel *hourLb; @property (nonatomic, strong) UILabel *hourSpaceLb; @property (nonatomic, strong) UILabel *minLb; @property (nonatomic, strong) UILabel *minSpaceLb; @property (nonatomic, strong) UILabel *secLb; @property (nonatomic, strong) NSTimer *timer; @end @implementation KWTimeEndView -(void)setTime:(NSTimeInterval)time { _time = time; [self showTime]; } - (void)stopTimer { if (self.timer != nil) { [self.timer invalidate]; self.timer = nil; } } - (void)startTimer { if (self.timer != nil) { [self.timer invalidate]; self.timer = nil; } self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:true]; } - (void)dealloc { [self stopTimer]; } -(void)timerAction { self.time -= 1; if (_time <= 0 && self.timer != nil) { [self stopTimer]; } } -(void)showTime { NSInteger hour = ((NSInteger)self.time)/(60*60); NSInteger sec = ((NSInteger)self.time)%60; NSInteger min = (((NSInteger)self.time) - hour*60*60 - sec)/60; self.hourLb.text = [NSString stringWithFormat:@"%02ld",hour]; self.minLb.text = [NSString stringWithFormat:@"%02ld",min]; self.secLb.text = [NSString stringWithFormat:@"%02ld",sec]; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self configSubV]; } return self; } -(void)configSubV { self.hourLb = [KWTimeEndView createTimeLB]; self.minLb = [KWTimeEndView createTimeLB]; self.secLb = [KWTimeEndView createTimeLB]; self.hourSpaceLb = [KWTimeEndView createSpaceLB]; self.minSpaceLb = [KWTimeEndView createSpaceLB]; UIStackView *contentStackV = [KWTimeEndView createStackV]; [contentStackV addArrangedSubview:_hourLb]; [contentStackV addArrangedSubview:_hourSpaceLb]; [contentStackV addArrangedSubview:_minLb]; [contentStackV addArrangedSubview:_minSpaceLb]; [contentStackV addArrangedSubview:_secLb]; [self addSubview:contentStackV]; [contentStackV mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self); }]; [self.hourLb mas_makeConstraints:^(MASConstraintMaker *make) { make.width.height.equalTo(@21); }]; [self.minLb mas_makeConstraints:^(MASConstraintMaker *make) { make.width.height.equalTo(@21); }]; [self.secLb mas_makeConstraints:^(MASConstraintMaker *make) { make.width.height.equalTo(@21); }]; [self.minSpaceLb mas_makeConstraints:^(MASConstraintMaker *make) { make.height.equalTo(@21); make.width.equalTo(@3); }]; [self.hourSpaceLb mas_makeConstraints:^(MASConstraintMaker *make) { make.height.equalTo(@21); make.width.equalTo(@3); }]; } + (UILabel *)createTimeLB { UILabel *lb = [[UILabel alloc] init]; lb.font = [UIFont fontWithName:Rob_Bold size:12]; lb.textColor = [UIColor whiteColor]; lb.textAlignment = NSTextAlignmentCenter; lb.backgroundColor = [UIColor blackColor]; return lb; } + (UILabel *)createSpaceLB { UILabel *lb = [[UILabel alloc] init]; lb.font = [UIFont fontWithName:Rob_Bold size:12]; lb.textColor = [UIColor blackColor]; lb.text = @":"; lb.textAlignment = NSTextAlignmentCenter; lb.backgroundColor = [UIColor clearColor]; return lb; } + (UIStackView *)createStackV { UIStackView *v = [[UIStackView alloc] init]; v.alignment = UIStackViewAlignmentFill; v.distribution = UIStackViewDistributionFill; v.spacing = 3; v.axis = UILayoutConstraintAxisHorizontal; return v; } @end