暫無描述

FKProCountDownView.m 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // FKProCountDownView.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 2016/11/14.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKProCountDownView.h"
  9. @implementation FKTimeUnitView
  10. - (instancetype)init {
  11. self = [super init];
  12. if (self) {
  13. self.layer.cornerRadius = 3;
  14. self.clipsToBounds = YES;
  15. self.backgroundColor = UIColorFromRGB(0xff2c6b);
  16. [self addSubview:self.timeLabel];
  17. [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  18. make.center.equalTo(self);
  19. }];
  20. }
  21. return self;
  22. }
  23. - (UILabel *)timeLabel {
  24. if (_timeLabel == nil) {
  25. _timeLabel = [[UILabel alloc]init];
  26. _timeLabel.textColor = UIColorFromRGB(0xffffff);
  27. _timeLabel.font = [UIFont systemFontOfSize:13];
  28. _timeLabel.textAlignment = NSTextAlignmentCenter;
  29. }
  30. return _timeLabel;
  31. }
  32. @end
  33. @interface FKProCountDownView ()
  34. @property (nonatomic, strong) FKTimeUnitView *dayUnitView;
  35. @property (nonatomic, strong) FKTimeUnitView *hourUnitView;
  36. @property (nonatomic, strong) FKTimeUnitView *minuteUnitView;
  37. @property (nonatomic, strong) FKTimeUnitView *secondsUnitView;
  38. @property (nonatomic, assign) NSTimeInterval totalTime;
  39. @property (nonatomic, assign) NSTimeInterval elapseTime;
  40. @property (nonatomic, strong) NSTimer *timer;
  41. @end
  42. @implementation FKProCountDownView
  43. /*
  44. // Only override drawRect: if you perform custom drawing.
  45. // An empty implementation adversely affects performance during animation.
  46. - (void)drawRect:(CGRect)rect {
  47. // Drawing code
  48. }
  49. */
  50. - (instancetype)init {
  51. self = [super init];
  52. if (self) {
  53. self.dayUnitView = [self makeTimeUnitView];
  54. self.hourUnitView = [self makeTimeUnitView];
  55. self.minuteUnitView = [self makeTimeUnitView];
  56. self.secondsUnitView = [self makeTimeUnitView];
  57. self.backgroundColor = UIColorFromRGB(0xffeae9);
  58. [self addAllSubviews];
  59. }
  60. return self;
  61. }
  62. - (void)dealloc {
  63. [self.timer invalidate];
  64. self.timer = nil;
  65. }
  66. - (void)startWithTimeInterval:(NSTimeInterval)ti {
  67. if (ti != self.totalTime) {
  68. [self.timer invalidate];
  69. self.timer = nil;
  70. self.totalTime = ti;
  71. self.elapseTime = 0;
  72. [self refreshTimeWithTimeInterval:ti];
  73. self.timer = [NSTimer timerWithTimeInterval:1.0
  74. target:self
  75. selector:@selector(timeCountDownCallback)
  76. userInfo:nil
  77. repeats:YES];
  78. [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
  79. }
  80. }
  81. - (void)timeCountDownCallback {
  82. self.elapseTime = (self.elapseTime + 1);
  83. NSTimeInterval remainTime = (self.totalTime - self.elapseTime);
  84. if (remainTime <= 0) {
  85. [self.timer invalidate];
  86. self.timer = nil;
  87. [[NSNotificationCenter defaultCenter] postNotificationName:PRODUCT_DETAIL_COUNT_TIME_ZERO
  88. object:nil];
  89. }
  90. [self refreshTimeWithTimeInterval:MAX(0, remainTime)];
  91. }
  92. - (void)refreshTimeWithTimeInterval:(NSTimeInterval)ti {
  93. NSDateComponents *componets = [FLStringHelper convertSecondToComponents:ti];
  94. self.dayUnitView.timeLabel.text = [NSString stringWithFormat:@"%02ld", componets.day];
  95. self.hourUnitView.timeLabel.text = [NSString stringWithFormat:@"%02ld", componets.hour];
  96. self.minuteUnitView.timeLabel.text = [NSString stringWithFormat:@"%02ld", componets.minute];
  97. self.secondsUnitView.timeLabel.text = [NSString stringWithFormat:@"%02ld", componets.second];
  98. }
  99. #pragma mark - Layout
  100. - (void)addAllSubviews {
  101. [self addSubview:self.descLabel];
  102. [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  103. make.centerX.equalTo(self);
  104. make.bottom.equalTo(self.mas_centerY).offset(-5);
  105. }];
  106. UIView *centerDotView = [self makeDotView];
  107. [self addSubview:centerDotView];
  108. [centerDotView mas_makeConstraints:^(MASConstraintMaker *make) {
  109. make.centerX.equalTo(self);
  110. make.top.equalTo(self.mas_centerY).offset(-3);
  111. make.size.mas_equalTo(CGSizeMake(8, 19));
  112. }];
  113. [self addSubview:self.hourUnitView];
  114. [self.hourUnitView mas_makeConstraints:^(MASConstraintMaker *make) {
  115. make.centerY.equalTo(centerDotView).offset(1);
  116. make.right.equalTo(centerDotView.mas_left);
  117. make.size.mas_equalTo(CGSizeMake(19, 19));
  118. }];
  119. [self addSubview:self.minuteUnitView];
  120. [self.minuteUnitView mas_makeConstraints:^(MASConstraintMaker *make) {
  121. make.centerY.equalTo(self.hourUnitView);
  122. make.left.equalTo(centerDotView.mas_right);
  123. make.size.mas_equalTo(CGSizeMake(19, 19));
  124. }];
  125. UIView *leftDotView = [self makeDotView];
  126. [self addSubview:leftDotView];
  127. [leftDotView mas_makeConstraints:^(MASConstraintMaker *make) {
  128. make.centerY.equalTo(centerDotView);
  129. make.right.equalTo(self.hourUnitView.mas_left);
  130. make.size.equalTo(centerDotView);
  131. }];
  132. [self addSubview:self.dayUnitView];
  133. [self.dayUnitView mas_makeConstraints:^(MASConstraintMaker *make) {
  134. make.centerY.equalTo(self.hourUnitView);
  135. make.right.equalTo(leftDotView.mas_left);
  136. make.size.mas_equalTo(CGSizeMake(19, 19));
  137. }];
  138. UIView *rightDotView = [self makeDotView];
  139. [self addSubview:rightDotView];
  140. [rightDotView mas_makeConstraints:^(MASConstraintMaker *make) {
  141. make.centerY.equalTo(centerDotView);
  142. make.left.equalTo(self.minuteUnitView.mas_right);
  143. make.size.equalTo(centerDotView);
  144. }];
  145. [self addSubview:self.secondsUnitView];
  146. [self.secondsUnitView mas_makeConstraints:^(MASConstraintMaker *make) {
  147. make.centerY.equalTo(self.hourUnitView);
  148. make.left.equalTo(rightDotView.mas_right);
  149. make.size.mas_equalTo(CGSizeMake(19, 19));
  150. }];
  151. }
  152. #pragma mark - Property
  153. - (UILabel *)descLabel {
  154. if (_descLabel == nil) {
  155. _descLabel = [[UILabel alloc]init];
  156. _descLabel.textColor = UIColorFromRGB(0xff2c6b);
  157. _descLabel.font = [UIFont systemFontOfSize:12];
  158. }
  159. return _descLabel;
  160. }
  161. - (FKTimeUnitView *)makeTimeUnitView {
  162. return [[FKTimeUnitView alloc] init];
  163. }
  164. - (UILabel *)makeDotView {
  165. UILabel *label = [UILabel new];
  166. label.textColor = UIColorFromRGB(0xff2c6b);
  167. label.font = [UIFont systemFontOfSize:13];
  168. label.textAlignment = NSTextAlignmentCenter;
  169. label.text = @":";
  170. return label;
  171. }
  172. @end