KWTimeEndView.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // KWTimeEndView.m
  3. // westkissMob
  4. //
  5. // Created by iOS on 2022/9/6.
  6. //
  7. #import "KWTimeEndView.h"
  8. @interface KWTimeEndView ()
  9. @property (nonatomic, strong) UILabel *hourLb;
  10. @property (nonatomic, strong) UILabel *hourSpaceLb;
  11. @property (nonatomic, strong) UILabel *minLb;
  12. @property (nonatomic, strong) UILabel *minSpaceLb;
  13. @property (nonatomic, strong) UILabel *secLb;
  14. @property (nonatomic, strong) NSTimer *timer;
  15. @end
  16. @implementation KWTimeEndView
  17. -(void)setTime:(NSTimeInterval)time {
  18. _time = time;
  19. [self showTime];
  20. }
  21. - (void)stopTimer {
  22. if (self.timer != nil) {
  23. [self.timer invalidate];
  24. self.timer = nil;
  25. }
  26. }
  27. - (void)startTimer {
  28. if (self.timer != nil) {
  29. [self.timer invalidate];
  30. self.timer = nil;
  31. }
  32. self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:true];
  33. }
  34. - (void)dealloc {
  35. [self stopTimer];
  36. }
  37. -(void)timerAction {
  38. self.time -= 1;
  39. if (_time <= 0 && self.timer != nil) {
  40. [self stopTimer];
  41. }
  42. }
  43. -(void)showTime {
  44. NSInteger hour = ((NSInteger)self.time)/(60*60);
  45. NSInteger sec = ((NSInteger)self.time)%60;
  46. NSInteger min = (((NSInteger)self.time) - hour*60*60 - sec)/60;
  47. self.hourLb.text = [NSString stringWithFormat:@"%02ld",hour];
  48. self.minLb.text = [NSString stringWithFormat:@"%02ld",min];
  49. self.secLb.text = [NSString stringWithFormat:@"%02ld",sec];
  50. }
  51. - (instancetype)initWithFrame:(CGRect)frame {
  52. self = [super initWithFrame:frame];
  53. if (self) {
  54. [self configSubV];
  55. }
  56. return self;
  57. }
  58. -(void)configSubV {
  59. self.hourLb = [KWTimeEndView createTimeLB];
  60. self.minLb = [KWTimeEndView createTimeLB];
  61. self.secLb = [KWTimeEndView createTimeLB];
  62. self.hourSpaceLb = [KWTimeEndView createSpaceLB];
  63. self.minSpaceLb = [KWTimeEndView createSpaceLB];
  64. UIStackView *contentStackV = [KWTimeEndView createStackV];
  65. [contentStackV addArrangedSubview:_hourLb];
  66. [contentStackV addArrangedSubview:_hourSpaceLb];
  67. [contentStackV addArrangedSubview:_minLb];
  68. [contentStackV addArrangedSubview:_minSpaceLb];
  69. [contentStackV addArrangedSubview:_secLb];
  70. [self addSubview:contentStackV];
  71. [contentStackV mas_makeConstraints:^(MASConstraintMaker *make) {
  72. make.center.equalTo(self);
  73. }];
  74. [self.hourLb mas_makeConstraints:^(MASConstraintMaker *make) {
  75. make.width.height.equalTo(@21);
  76. }];
  77. [self.minLb mas_makeConstraints:^(MASConstraintMaker *make) {
  78. make.width.height.equalTo(@21);
  79. }];
  80. [self.secLb mas_makeConstraints:^(MASConstraintMaker *make) {
  81. make.width.height.equalTo(@21);
  82. }];
  83. [self.minSpaceLb mas_makeConstraints:^(MASConstraintMaker *make) {
  84. make.height.equalTo(@21);
  85. make.width.equalTo(@3);
  86. }];
  87. [self.hourSpaceLb mas_makeConstraints:^(MASConstraintMaker *make) {
  88. make.height.equalTo(@21);
  89. make.width.equalTo(@3);
  90. }];
  91. }
  92. + (UILabel *)createTimeLB {
  93. UILabel *lb = [[UILabel alloc] init];
  94. lb.font = [UIFont fontWithName:Rob_Bold size:12];
  95. lb.textColor = [UIColor whiteColor];
  96. lb.textAlignment = NSTextAlignmentCenter;
  97. lb.backgroundColor = [UIColor blackColor];
  98. return lb;
  99. }
  100. + (UILabel *)createSpaceLB {
  101. UILabel *lb = [[UILabel alloc] init];
  102. lb.font = [UIFont fontWithName:Rob_Bold size:12];
  103. lb.textColor = [UIColor blackColor];
  104. lb.text = @":";
  105. lb.textAlignment = NSTextAlignmentCenter;
  106. lb.backgroundColor = [UIColor clearColor];
  107. return lb;
  108. }
  109. + (UIStackView *)createStackV {
  110. UIStackView *v = [[UIStackView alloc] init];
  111. v.alignment = UIStackViewAlignmentFill;
  112. v.distribution = UIStackViewDistributionFill;
  113. v.spacing = 3;
  114. v.axis = UILayoutConstraintAxisHorizontal;
  115. return v;
  116. }
  117. @end