《省钱达人》与《猎豆优选》UI相同版。域名tbk

DRCountDownView.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //
  2. // DRCountDownView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/17.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRCountDownView.h"
  9. #define TimerFont [UIFont systemFontOfSize:13]
  10. @interface DRCountDownView (){
  11. NSUInteger expiresTime;
  12. NSUInteger nowTime;
  13. NSDate *date1970;
  14. NSDateFormatter *dateFormatter;
  15. NSUInteger lastSeconds;
  16. NSInteger aDay;
  17. }
  18. @property (nonatomic, strong) dispatch_source_t timer;
  19. /**小时*/
  20. @property (nonatomic,strong) UIButton *hourBtn;
  21. @property (nonatomic,strong) UIButton *hourBtn2;
  22. /**分钟*/
  23. @property (nonatomic,strong) UIButton *minuteBtn;
  24. @property (nonatomic,strong) UIButton *minuteBtn2;
  25. /**秒*/
  26. @property (nonatomic,strong) UIButton *secondBtn;
  27. @property (nonatomic,strong) UIButton *secondBtn2;
  28. /**:*/
  29. @property (nonatomic,strong) UIButton *textBtn;
  30. @property (nonatomic,strong) UIButton *textBtn1;
  31. @property (nonatomic,strong) UIButton *textBtn2;
  32. @property (nonatomic,strong) UIButton *textBtn3;
  33. @end
  34. static DRCountDownView *_countDownView = nil;
  35. @implementation DRCountDownView
  36. + (instancetype)shareInstace {
  37. static dispatch_once_t onceToken;
  38. dispatch_once(&onceToken, ^{
  39. _countDownView = [[DRCountDownView alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
  40. });
  41. return _countDownView;
  42. }
  43. - (instancetype)initWithFrame:(CGRect)frame andEndTimer:(NSString *)endTimer
  44. {
  45. NSDate *date = [NSDate date];
  46. NSTimeInterval timeinterval =[date timeIntervalSince1970];
  47. NSTimeInterval endTime = timeinterval + endTimer.longLongValue;
  48. // NSString *timeString2 = [NSString stringWithFormat:@"%.0f", a2];
  49. expiresTime = endTime;
  50. return [self initWithFrame:frame];
  51. }
  52. - (instancetype)initWithFrame:(CGRect)frame {
  53. self = [super initWithFrame:frame];
  54. if (self) {
  55. date1970 = [NSDate dateWithTimeIntervalSince1970:0];
  56. dateFormatter = [[NSDateFormatter alloc] init];
  57. [dateFormatter setDateFormat:@"HH mm ss"];
  58. aDay = 86399;
  59. expiresTime = 0;
  60. nowTime = 0;
  61. [self addSubview:self.textBtn];
  62. [self addSubview:self.hourBtn];
  63. [self addSubview:self.hourBtn2];
  64. [self addSubview:self.textBtn1];
  65. [self addSubview:self.minuteBtn];
  66. [self addSubview:self.minuteBtn2];
  67. [self addSubview:self.textBtn2];
  68. [self addSubview:self.secondBtn];
  69. [self addSubview:self.secondBtn2];
  70. [self addSubview:self.textBtn3];
  71. }
  72. return self;
  73. }
  74. - (void)countDown {
  75. // 从1970年到现在的时间(秒)
  76. NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
  77. NSTimeInterval second =[dat timeIntervalSince1970];
  78. NSString *starTimer = [NSString stringWithFormat:@"%.0f", second];
  79. nowTime = starTimer.longLongValue;
  80. __block NSUInteger timeout= expiresTime; //倒计时时间
  81. NSString *curStr = [self homeLimitTimeString];
  82. [self labelValueHandler:curStr];
  83. if (_timer) {
  84. dispatch_source_cancel(_timer);
  85. }
  86. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  87. _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
  88. dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1*NSEC_PER_SEC, 0); //每秒执行
  89. dispatch_source_set_event_handler(_timer, ^{
  90. if(timeout<=starTimer.longLongValue){ //倒计时结束,关闭
  91. dispatch_source_cancel(_timer);
  92. dispatch_async(dispatch_get_main_queue(), ^{
  93. });
  94. if (self.delegate && [self.delegate respondsToSelector:@selector(countViewTimeOutAction)]) {
  95. [self.delegate countViewTimeOutAction];
  96. }
  97. }else{
  98. NSString *curStr1 = [self homeLimitTimeString];
  99. dispatch_async(dispatch_get_main_queue(), ^{
  100. [self labelValueHandler:curStr1];
  101. });
  102. timeout--;
  103. }
  104. });
  105. dispatch_resume(_timer);
  106. }
  107. - (void)setEndTimer:(NSString *)endTimer {
  108. NSDate *date = [NSDate date];
  109. NSTimeInterval timeinterval =[date timeIntervalSince1970];
  110. NSString *endTimeStr = [NSString stringWithFormat:@"%.0f",timeinterval];
  111. NSTimeInterval endTime = endTimeStr.longLongValue + endTimer.longLongValue/1000;
  112. expiresTime = endTime; //到期的时间戳
  113. // NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
  114. // NSTimeInterval second =[dat timeIntervalSince1970];
  115. // NSString *starTimer = [NSString stringWithFormat:@"%.0f", second];
  116. // nowTime = starTimer.longLongValue;
  117. [self countDown];
  118. }
  119. -(NSString*)homeLimitTimeString{
  120. NSTimeInterval timeActualValue;
  121. if (expiresTime<nowTime) {
  122. return @"00 00 00";
  123. }
  124. NSTimeInterval diffTime = expiresTime - nowTime;
  125. // NSLog(@"%ld",(expiresTime - nowTime)/3600/24); /// 这里可以设置天数
  126. timeActualValue = diffTime;
  127. nowTime++;
  128. NSDate *curDate = [date1970 dateByAddingTimeInterval:timeActualValue];
  129. NSTimeZone *zone = [NSTimeZone timeZoneWithAbbreviation:@"GMT-0800"];
  130. NSInteger interval = [zone secondsFromGMTForDate: curDate];
  131. NSDate *localeDate = [curDate dateByAddingTimeInterval: interval];
  132. NSString *destDateString = [dateFormatter stringFromDate:localeDate];
  133. return destDateString;
  134. }
  135. -(void)labelValueHandler:(NSString*)text{
  136. NSArray *titleArr = [text componentsSeparatedByString:@" "];
  137. NSString *hourStr = titleArr[0];
  138. NSString *minStr = titleArr[1];
  139. NSString *secondStr = titleArr[2];
  140. [self.hourBtn setTitle:[hourStr substringWithRange:NSMakeRange(0, 1)] forState:UIControlStateNormal];
  141. [self.hourBtn2 setTitle:[hourStr substringWithRange:NSMakeRange(1, 1)] forState:UIControlStateNormal];
  142. [self.minuteBtn setTitle:[minStr substringWithRange:NSMakeRange(0, 1)] forState:UIControlStateNormal];
  143. [self.minuteBtn2 setTitle:[minStr substringWithRange:NSMakeRange(1, 1)] forState:UIControlStateNormal];
  144. [self.secondBtn setTitle:[secondStr substringWithRange:NSMakeRange(0, 1)] forState:UIControlStateNormal];
  145. [self.secondBtn2 setTitle:[secondStr substringWithRange:NSMakeRange(1, 1)] forState:UIControlStateNormal];
  146. }
  147. #pragma mark =============== layzer ==================
  148. - (UIButton *)textBtn
  149. {
  150. if (_textBtn == nil) {
  151. _textBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  152. _textBtn.frame = CGRectMake(0, 0, 50, self.frame.size.height);
  153. _textBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
  154. [_textBtn setTitle:@"距更新" forState:UIControlStateNormal];
  155. _textBtn.titleLabel.font = [UIFont systemFontOfSize:12];
  156. [_textBtn setTitleColor:[UIColor YHColorWithHex:0x999999] forState:UIControlStateNormal];
  157. }
  158. return _textBtn;
  159. }
  160. - (UIButton *)hourBtn
  161. {
  162. if (_hourBtn == nil) {
  163. _hourBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  164. _hourBtn.frame = CGRectMake(CGRectGetMaxX(_textBtn.frame), 0, 12, self.frame.size.height);
  165. _hourBtn.backgroundColor = [UIColor YHColorWithHex:0x444444];
  166. _hourBtn.titleLabel.font = TimerFont;
  167. _hourBtn.layer.cornerRadius = 3;
  168. }
  169. return _hourBtn;
  170. }
  171. - (UIButton *)hourBtn2
  172. {
  173. if (_hourBtn2 == nil) {
  174. _hourBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
  175. _hourBtn2.frame = CGRectMake(CGRectGetMaxX(_hourBtn.frame)+2, 0, 12, self.frame.size.height);
  176. _hourBtn2.backgroundColor = [UIColor YHColorWithHex:0x444444];
  177. _hourBtn2.titleLabel.font = TimerFont;
  178. _hourBtn2.layer.cornerRadius = 3;
  179. }
  180. return _hourBtn2;
  181. }
  182. - (UIButton *)textBtn1
  183. {
  184. if (_textBtn1 == nil) {
  185. _textBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
  186. _textBtn1.frame = CGRectMake(CGRectGetMaxX(_hourBtn2.frame), 0, 20, _hourBtn.frame.size.height);
  187. _textBtn1.titleLabel.textAlignment = NSTextAlignmentCenter;
  188. [_textBtn1 setTitle:@"时" forState:UIControlStateNormal];
  189. _textBtn1.titleLabel.font = [UIFont systemFontOfSize:12];
  190. [_textBtn1 setTitleColor:[UIColor YHColorWithHex:0x999999] forState:UIControlStateNormal];
  191. }
  192. return _textBtn1;
  193. }
  194. - (UIButton *)minuteBtn
  195. {
  196. if (_minuteBtn == nil) {
  197. _minuteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  198. _minuteBtn.frame = CGRectMake(CGRectGetMaxX(_textBtn1.frame),0 , 12, _hourBtn.frame.size.height);
  199. _minuteBtn.titleLabel.font = TimerFont;
  200. _minuteBtn.backgroundColor = [UIColor YHColorWithHex:0x444444];
  201. _minuteBtn.layer.cornerRadius = 3;
  202. }
  203. return _minuteBtn;
  204. }
  205. - (UIButton *)minuteBtn2
  206. {
  207. if (_minuteBtn2 == nil) {
  208. _minuteBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
  209. _minuteBtn2.frame = CGRectMake(CGRectGetMaxX(_minuteBtn.frame)+2,0 , 12, _hourBtn.frame.size.height);
  210. _minuteBtn2.titleLabel.font = TimerFont;
  211. _minuteBtn2.backgroundColor = [UIColor YHColorWithHex:0x444444];
  212. _minuteBtn2.layer.cornerRadius = 3;
  213. }
  214. return _minuteBtn2;
  215. }
  216. - (UIButton *)textBtn2
  217. {
  218. if (_textBtn2 == nil) {
  219. _textBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
  220. _textBtn2.frame = CGRectMake(CGRectGetMaxX(_minuteBtn2.frame), 0, 20, _hourBtn.frame.size.height);
  221. _textBtn2.titleLabel.textAlignment = NSTextAlignmentCenter;
  222. [_textBtn2 setTitle:@"分" forState:UIControlStateNormal];
  223. _textBtn2.titleLabel.font = [UIFont systemFontOfSize:12];
  224. [_textBtn2 setTitleColor:[UIColor YHColorWithHex:0x999999] forState:UIControlStateNormal];
  225. }
  226. return _textBtn2;
  227. }
  228. - (UIButton *)secondBtn
  229. {
  230. if (_secondBtn == nil) {
  231. _secondBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  232. _secondBtn.frame = CGRectMake(CGRectGetMaxX(_textBtn2.frame), 0, 12,_hourBtn.frame.size.height);
  233. _secondBtn.backgroundColor = [UIColor YHColorWithHex:0x444444];
  234. _secondBtn.titleLabel.font = TimerFont;
  235. _secondBtn.layer.cornerRadius = 3;
  236. }
  237. return _secondBtn;
  238. }
  239. - (UIButton *)secondBtn2
  240. {
  241. if (_secondBtn2 == nil) {
  242. _secondBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
  243. _secondBtn2.frame = CGRectMake(CGRectGetMaxX(_secondBtn.frame)+2, 0, 12,_hourBtn.frame.size.height);
  244. _secondBtn2.backgroundColor = [UIColor YHColorWithHex:0x444444];
  245. _secondBtn2.titleLabel.font = TimerFont;
  246. _secondBtn2.layer.cornerRadius = 3;
  247. }
  248. return _secondBtn2;
  249. }
  250. - (UIButton *)textBtn3
  251. {
  252. if (_textBtn3 == nil) {
  253. _textBtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
  254. _textBtn3.frame = CGRectMake(CGRectGetMaxX(_secondBtn2.frame), 0, 20, _hourBtn.frame.size.height);
  255. _textBtn3.titleLabel.textAlignment = NSTextAlignmentCenter;
  256. [_textBtn3 setTitle:@"秒" forState:UIControlStateNormal];
  257. _textBtn3.titleLabel.font = [UIFont systemFontOfSize:12];
  258. [_textBtn3 setTitleColor:[UIColor YHColorWithHex:0x999999] forState:UIControlStateNormal];
  259. }
  260. return _textBtn3;
  261. }
  262. @end