MZTimerLabel.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // MZTimerLabel.m
  3. // Version 0.2
  4. // Created by MineS Chan on 2013-10-16
  5. // Updated 2013-11-05
  6. // This code is distributed under the terms and conditions of the MIT license.
  7. // Copyright (c) 2013 MineS Chan
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #import "MZTimerLabel.h"
  27. #define kDefaultTimeFormat @"HH:mm:ss"
  28. #define kDefaultFireIntervalNormal 0.1
  29. #define kDefaultFireIntervalHighUse 0.02
  30. @interface MZTimerLabel()
  31. @property (strong) NSTimer *timer;
  32. -(void)setup;
  33. -(void)updateLabel:(NSTimer*)timer;
  34. @end
  35. #pragma mark - Initialize method
  36. @implementation MZTimerLabel
  37. -(id)init{
  38. self = [super init];
  39. if (self) {
  40. _timeLabel = self;
  41. _timerType = MZTimerLabelTypeStopWatch;
  42. }
  43. return self;
  44. }
  45. -(id)initWithTimerType:(MZTimerLabelType)theType{
  46. return [self initWithLabel:nil andTimerType:theType];
  47. }
  48. -(id)initWithLabel:(UILabel *)theLabel andTimerType:(MZTimerLabelType)theType
  49. {
  50. self = [super init];
  51. if(self){
  52. _timeLabel = theLabel;
  53. _timerType = theType;
  54. [self setup];
  55. }
  56. return self;
  57. }
  58. -(id)initWithLabel:(UILabel*)theLabel{
  59. self = [super init];
  60. if(self){
  61. _timeLabel = theLabel;
  62. _timerType = MZTimerLabelTypeStopWatch;
  63. [self setup];
  64. }
  65. return self;
  66. }
  67. #pragma mark - Getter and Setter Method
  68. -(void)setStopWatchTime:(NSTimeInterval)time{
  69. timeUserValue = (time < 0) ? 0 : time;
  70. if(timeUserValue > 0){
  71. startCountDate = [[NSDate date] dateByAddingTimeInterval:(timeUserValue<0)?0:-timeUserValue];
  72. [self start];
  73. [self updateLabel:nil];
  74. [self pause];
  75. }
  76. }
  77. -(void)setCountDownTime:(NSTimeInterval)time{
  78. timeUserValue = time;
  79. timeToCountOff = [date1970 dateByAddingTimeInterval:time];
  80. [self updateLabel:nil];
  81. }
  82. -(void)setTimeFormat:(NSString *)timeFormat{
  83. _timeFormat = timeFormat;
  84. if ([_timeFormat length] != 0) {
  85. _timeFormat = timeFormat;
  86. }
  87. [self updateLabel:nil];
  88. }
  89. #pragma mark - Timer Control Method
  90. -(void)start{
  91. [self setup];
  92. if(_timer == nil){
  93. if ([_timeFormat rangeOfString:@"SS"].location != NSNotFound) {
  94. _timer = [NSTimer scheduledTimerWithTimeInterval:kDefaultFireIntervalHighUse target:self selector:@selector(updateLabel:) userInfo:nil repeats:YES];
  95. }else{
  96. _timer = [NSTimer scheduledTimerWithTimeInterval:kDefaultFireIntervalNormal target:self selector:@selector(updateLabel:) userInfo:nil repeats:YES];
  97. }
  98. [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  99. if(startCountDate == nil){
  100. startCountDate = [NSDate date];
  101. if (_timerType == MZTimerLabelTypeStopWatch && timeUserValue > 0) {
  102. startCountDate = [startCountDate dateByAddingTimeInterval:(timeUserValue<0)?0:-timeUserValue];
  103. }
  104. }
  105. if(pausedTime != nil){
  106. NSTimeInterval countedTime = [pausedTime timeIntervalSinceDate:startCountDate];
  107. startCountDate = [[NSDate date] dateByAddingTimeInterval:-countedTime];
  108. pausedTime = nil;
  109. }
  110. _counting = YES;
  111. [_timer fire];
  112. }
  113. }
  114. #if NS_BLOCKS_AVAILABLE
  115. -(void)startWithEndingBlock:(void(^)(NSTimeInterval))end{
  116. [self start];
  117. endedBlock = end;
  118. }
  119. #endif
  120. -(void)pause{
  121. [_timer invalidate];
  122. _timer = nil;
  123. _counting = NO;
  124. pausedTime = [NSDate date];
  125. }
  126. -(void)reset{
  127. pausedTime = nil;
  128. if(_timerType == MZTimerLabelTypeStopWatch) timeUserValue = 0;
  129. if(_counting){
  130. startCountDate = [NSDate date];
  131. }else{
  132. startCountDate = nil;
  133. }
  134. [self updateLabel:nil];
  135. }
  136. #pragma mark - Private method
  137. -(void)setup{
  138. if ([_timeFormat length] == 0) {
  139. _timeFormat = kDefaultTimeFormat;
  140. }
  141. if(_timeLabel == nil){
  142. _timeLabel = self;
  143. }
  144. date1970 = [NSDate dateWithTimeIntervalSince1970:0];
  145. _timeLabel.adjustsFontSizeToFitWidth = YES;
  146. [self updateLabel:nil];
  147. }
  148. -(void)updateLabel:(NSTimer*)timer{
  149. NSTimeInterval timeDiff = [[[NSDate alloc] init] timeIntervalSinceDate:startCountDate];
  150. NSDate *timeToShow = [NSDate date];
  151. if(_timerType == MZTimerLabelTypeStopWatch){
  152. if (_counting) {
  153. timeToShow = [date1970 dateByAddingTimeInterval:timeDiff];
  154. }else{
  155. timeToShow = [date1970 dateByAddingTimeInterval:(!startCountDate)?0:timeDiff];
  156. }
  157. if([_delegate respondsToSelector:@selector(timerLabel:countingTo:timertype:)]){
  158. [_delegate timerLabel:self countingTo:timeDiff timertype:_timerType];
  159. }
  160. }else{
  161. //timer now
  162. if (_counting) {
  163. if([_delegate respondsToSelector:@selector(timerLabel:countingTo:timertype:)]){
  164. NSTimeInterval timeLeft = timeUserValue - timeDiff;
  165. [_delegate timerLabel:self countingTo:timeLeft timertype:_timerType];
  166. }
  167. if(abs(timeDiff) >= timeUserValue){
  168. [self pause];
  169. timeToShow = [date1970 dateByAddingTimeInterval:0];
  170. pausedTime = nil;
  171. startCountDate = nil;
  172. if([_delegate respondsToSelector:@selector(timerLabel:finshedCountDownTimerWithTime:)]){
  173. [_delegate timerLabel:self finshedCountDownTimerWithTime:timeUserValue];
  174. }
  175. #if NS_BLOCKS_AVAILABLE
  176. if(endedBlock != nil){
  177. endedBlock(timeUserValue);
  178. }
  179. #endif
  180. if(_resetTimerAfterFinish){
  181. [self reset];
  182. return;
  183. }
  184. }else{
  185. timeToShow = [timeToCountOff dateByAddingTimeInterval:(timeDiff*-1)];
  186. }
  187. }else{
  188. timeToShow = timeToCountOff;
  189. }
  190. }
  191. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  192. [dateFormatter setDateFormat:_timeFormat];
  193. [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
  194. NSString *strDate = [dateFormatter stringFromDate:timeToShow];
  195. _timeLabel.text = strDate;
  196. }
  197. @end