UICountingLabel.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #import <QuartzCore/QuartzCore.h>
  2. #import "UICountingLabel.h"
  3. #if !__has_feature(objc_arc)
  4. #error UICountingLabel is ARC only. Either turn on ARC for the project or use -fobjc-arc flag
  5. #endif
  6. #pragma mark - UILabelCounter
  7. #ifndef kUILabelCounterRate
  8. #define kUILabelCounterRate 3.0
  9. #endif
  10. @protocol UILabelCounter<NSObject>
  11. -(CGFloat)update:(CGFloat)t;
  12. @end
  13. @interface UILabelCounterLinear : NSObject<UILabelCounter>
  14. @end
  15. @interface UILabelCounterEaseIn : NSObject<UILabelCounter>
  16. @end
  17. @interface UILabelCounterEaseOut : NSObject<UILabelCounter>
  18. @end
  19. @interface UILabelCounterEaseInOut : NSObject<UILabelCounter>
  20. @end
  21. @implementation UILabelCounterLinear
  22. -(CGFloat)update:(CGFloat)t
  23. {
  24. return t;
  25. }
  26. @end
  27. @implementation UILabelCounterEaseIn
  28. -(CGFloat)update:(CGFloat)t
  29. {
  30. return powf(t, kUILabelCounterRate);
  31. }
  32. @end
  33. @implementation UILabelCounterEaseOut
  34. -(CGFloat)update:(CGFloat)t{
  35. return 1.0-powf((1.0-t), kUILabelCounterRate);
  36. }
  37. @end
  38. @implementation UILabelCounterEaseInOut
  39. -(CGFloat) update: (CGFloat) t
  40. {
  41. t *= 2;
  42. if (t < 1)
  43. return 0.5f * powf (t, kUILabelCounterRate);
  44. else
  45. return 0.5f * (2.0f - powf(2.0 - t, kUILabelCounterRate));
  46. }
  47. @end
  48. #pragma mark - UICountingLabel
  49. @interface UICountingLabel ()
  50. @property CGFloat startingValue;
  51. @property CGFloat destinationValue;
  52. @property NSTimeInterval progress;
  53. @property NSTimeInterval lastUpdate;
  54. @property NSTimeInterval totalTime;
  55. @property CGFloat easingRate;
  56. @property (nonatomic, strong) CADisplayLink *timer;
  57. @property (nonatomic, strong) id<UILabelCounter> counter;
  58. @end
  59. @implementation UICountingLabel
  60. -(void)countFrom:(CGFloat)value to:(CGFloat)endValue {
  61. if (self.animationDuration == 0.0f) {
  62. self.animationDuration = 2.0f;
  63. }
  64. [self countFrom:value to:endValue withDuration:self.animationDuration];
  65. }
  66. -(void)countFrom:(CGFloat)startValue to:(CGFloat)endValue withDuration:(NSTimeInterval)duration {
  67. self.startingValue = startValue;
  68. self.destinationValue = endValue;
  69. // remove any (possible) old timers
  70. [self.timer invalidate];
  71. self.timer = nil;
  72. if (duration == 0.0) {
  73. // No animation
  74. [self setTextValue:endValue];
  75. [self runCompletionBlock];
  76. return;
  77. }
  78. self.easingRate = 3.0f;
  79. self.progress = 0;
  80. self.totalTime = duration;
  81. self.lastUpdate = [NSDate timeIntervalSinceReferenceDate];
  82. if(self.format == nil)
  83. self.format = @"%f";
  84. switch(self.method)
  85. {
  86. case UILabelCountingMethodLinear:
  87. self.counter = [[UILabelCounterLinear alloc] init];
  88. break;
  89. case UILabelCountingMethodEaseIn:
  90. self.counter = [[UILabelCounterEaseIn alloc] init];
  91. break;
  92. case UILabelCountingMethodEaseOut:
  93. self.counter = [[UILabelCounterEaseOut alloc] init];
  94. break;
  95. case UILabelCountingMethodEaseInOut:
  96. self.counter = [[UILabelCounterEaseInOut alloc] init];
  97. break;
  98. }
  99. CADisplayLink *timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateValue:)];
  100. timer.frameInterval = 2;
  101. [timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
  102. [timer addToRunLoop:[NSRunLoop mainRunLoop] forMode:UITrackingRunLoopMode];
  103. self.timer = timer;
  104. }
  105. - (void)countFromCurrentValueTo:(CGFloat)endValue {
  106. [self countFrom:[self currentValue] to:endValue];
  107. }
  108. - (void)countFromCurrentValueTo:(CGFloat)endValue withDuration:(NSTimeInterval)duration {
  109. [self countFrom:[self currentValue] to:endValue withDuration:duration];
  110. }
  111. - (void)countFromZeroTo:(CGFloat)endValue {
  112. [self countFrom:0.0f to:endValue];
  113. }
  114. - (void)countFromZeroTo:(CGFloat)endValue withDuration:(NSTimeInterval)duration {
  115. [self countFrom:0.0f to:endValue withDuration:duration];
  116. }
  117. - (void)updateValue:(NSTimer *)timer {
  118. // update progress
  119. NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
  120. self.progress += now - self.lastUpdate;
  121. self.lastUpdate = now;
  122. if (self.progress >= self.totalTime) {
  123. [self.timer invalidate];
  124. self.timer = nil;
  125. self.progress = self.totalTime;
  126. }
  127. [self setTextValue:[self currentValue]];
  128. if (self.progress == self.totalTime) {
  129. [self runCompletionBlock];
  130. }
  131. }
  132. - (void)setTextValue:(CGFloat)value
  133. {
  134. if (self.attributedFormatBlock != nil) {
  135. self.attributedText = self.attributedFormatBlock(value);
  136. }
  137. else if(self.formatBlock != nil)
  138. {
  139. self.text = self.formatBlock(value);
  140. }
  141. else
  142. {
  143. // check if counting with ints - cast to int
  144. if([self.format rangeOfString:@"%(.*)d" options:NSRegularExpressionSearch].location != NSNotFound || [self.format rangeOfString:@"%(.*)i"].location != NSNotFound )
  145. {
  146. self.text = [NSString stringWithFormat:self.format,(int)value];
  147. }
  148. else
  149. {
  150. self.text = [NSString stringWithFormat:self.format,value];
  151. }
  152. }
  153. }
  154. - (void)setFormat:(NSString *)format {
  155. _format = format;
  156. // update label with new format
  157. [self setTextValue:self.currentValue];
  158. }
  159. - (void)runCompletionBlock {
  160. if (self.completionBlock) {
  161. self.completionBlock();
  162. self.completionBlock = nil;
  163. }
  164. }
  165. - (CGFloat)currentValue {
  166. if (self.progress >= self.totalTime) {
  167. return self.destinationValue;
  168. }
  169. CGFloat percent = self.progress / self.totalTime;
  170. CGFloat updateVal = [self.counter update:percent];
  171. return self.startingValue + (updateVal * (self.destinationValue - self.startingValue));
  172. }
  173. @end