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

SPScrollNumLabel.m 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // SPScrollNumLabel.m
  3. // SPScrollNumLabel
  4. //
  5. // Created by Tree on 2017/11/21.
  6. // Copyright © 2017年 Tr2e. All rights reserved.
  7. //
  8. #import "SPScrollNumLabel.h"
  9. @interface SPScrollNumLabel (){
  10. BOOL has_centerPoint_detect;
  11. BOOL is_initializeFromXib;
  12. }
  13. @property (nonatomic, strong) UIColor *sp_textColor;// default is black
  14. @property (nonatomic, copy) NSArray <NSString *> *sepNumArr;
  15. @property (nonatomic, copy) NSArray <UILabel *> *sepLabelArr;
  16. @end
  17. @implementation SPScrollNumLabel
  18. // initialize
  19. - (instancetype)initWithCoder:(NSCoder *)aDecoder{
  20. if (self = [super initWithCoder:aDecoder]) {
  21. [self initailizeConfig];
  22. }
  23. return self;
  24. }
  25. - (void)layoutSubviews{
  26. [super layoutSubviews];
  27. }
  28. - (void)willMoveToWindow:(UIWindow *)newWindow{
  29. [super willMoveToWindow:newWindow];
  30. if (!_targetNumber && !self.isCommonLabel) {
  31. self.targetNumber = 0;
  32. }
  33. }
  34. - (void)initailizeConfig{
  35. is_initializeFromXib = YES;
  36. if (!self.isCommonLabel) {
  37. self.sp_textColor = self.textColor;
  38. self.textColor = [UIColor clearColor];
  39. }
  40. }
  41. // function
  42. - (void)increaseNumber:(NSInteger)increasedNum{
  43. self.targetNumber += increasedNum;
  44. }
  45. - (void)decreaseNumber:(NSInteger)decreasedNum{
  46. self.targetNumber -= decreasedNum;
  47. }
  48. - (void)operateWithTargetString:(NSString *)string{
  49. __weak typeof(self) ws = self;
  50. [self seprateStringToSingleWithTarget:string operateHandler:^(NSMutableArray<NSString *> *sepStrArr, NSMutableArray<UILabel *> *sepLabelArr) {
  51. [ws showScrollAnimiationWithSepStrArr:sepStrArr sepLableArr:sepLabelArr];
  52. }];
  53. }
  54. - (void)showScrollAnimiationWithSepStrArr:(NSArray <NSString *>*)sepStrArr sepLableArr:(NSArray <UILabel *> *)sepLabelArr{
  55. NSInteger targetLength = sepLabelArr.count;
  56. NSInteger hisLength = self.sepLabelArr.count;
  57. for (NSInteger i = 0; i < targetLength; i ++) {
  58. NSInteger targetIndex = targetLength - i - 1;
  59. NSInteger hisTargetIndex = hisLength - i - 1;
  60. if (i < hisLength) {
  61. if (![sepStrArr[targetIndex] isEqualToString:self.sepNumArr[hisTargetIndex]]) {
  62. BOOL increase = sepStrArr[targetIndex].integerValue > self.sepNumArr[hisTargetIndex].integerValue;
  63. // his label
  64. UILabel *hisLabel = self.sepLabelArr[hisTargetIndex];
  65. CGRect hisTargetFrame = CGRectOffset(hisLabel.frame, 0, (increase?-1:1)*hisLabel.frame.size.height);
  66. // new label
  67. UILabel *label = sepLabelArr[targetIndex];
  68. CGRect frame = label.frame;
  69. CGRect targetFrame = CGRectOffset(frame, 0, (increase?1:-1)*label.frame.size.height);
  70. label.frame = targetFrame;
  71. label.alpha = 0;
  72. [UIView animateWithDuration:self.animateDuration?self.animateDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  73. hisLabel.frame = hisTargetFrame;
  74. label.frame = frame;
  75. hisLabel.alpha = 0;
  76. label.alpha = 1;
  77. } completion:^(BOOL finished) {
  78. [hisLabel removeFromSuperview];
  79. }];
  80. }else{
  81. [self.sepLabelArr[hisTargetIndex] removeFromSuperview];
  82. }
  83. }else{
  84. UILabel *label = sepLabelArr[targetIndex];
  85. CGRect frame = label.frame;
  86. label.alpha = 0;
  87. label.frame = CGRectOffset(frame, 0, -frame.size.height);
  88. [UIView animateWithDuration:self.animateDuration?self.animateDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:2 options:UIViewAnimationOptionCurveEaseOut animations:^{
  89. label.frame = frame;
  90. label.alpha = 1;
  91. } completion:nil];
  92. }
  93. }
  94. if (hisLength > targetLength) {
  95. for (int i = 0; i < hisLength - targetLength; i ++) {
  96. UILabel *label = self.sepLabelArr[i];
  97. CGRect hisTargetFrame = CGRectOffset(label.frame, 0, -1*label.frame.size.height);
  98. [UIView animateWithDuration:self.animateDuration?self.animateDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  99. label.frame = hisTargetFrame;
  100. label.alpha = 0;
  101. } completion:^(BOOL finished) {
  102. [label removeFromSuperview];
  103. }];
  104. }
  105. }
  106. self.sepNumArr = sepStrArr;
  107. self.sepLabelArr = sepLabelArr;
  108. }
  109. // private operation
  110. - (UILabel *)createLabelWithString:(NSString *)numberStr{
  111. UILabel *label = [[UILabel alloc] init];
  112. label.font = self.font;
  113. label.textColor = self.sp_textColor?self.sp_textColor:self.textColor;
  114. label.textAlignment = NSTextAlignmentLeft;
  115. label.text = numberStr;
  116. [label sizeToFit];
  117. return label;
  118. }
  119. - (void)seprateStringToSingleWithTarget:(NSString *)target operateHandler:(void (^) (NSMutableArray<NSString *> *sepStrArr,NSMutableArray<UILabel *> *sepLabelArr)) handler{
  120. NSMutableArray <NSString *> * sepStrArr = [[NSMutableArray alloc] initWithCapacity:target.length];
  121. NSMutableArray <UILabel *> * sepLabelArr = [[NSMutableArray alloc] initWithCapacity:sepStrArr.count];
  122. for (NSInteger i = 0 ; i < target.length; i ++) {
  123. // str
  124. NSString *subStr = [target substringWithRange:NSMakeRange(i, 1)];
  125. [sepStrArr addObject:subStr];
  126. // label
  127. UILabel *label = [self createLabelWithString:subStr];
  128. CGFloat labelHeight = label.frame.size.height;
  129. CGFloat selfHeight = self.frame.size.height;
  130. label.frame = (CGRect){CGPointMake(sepLabelArr.count?CGRectGetMaxX(sepLabelArr.lastObject.frame):0, (labelHeight < selfHeight ? (selfHeight - labelHeight)/2: 0)),label.frame.size};
  131. [sepLabelArr addObject:label];
  132. [self addSubview:label];
  133. }
  134. CGFloat labelHeight = sepLabelArr.lastObject.frame.size.height;
  135. CGFloat selfHeight = self.frame.size.height;
  136. CGRect changedFrame = (CGRect){self.frame.origin,CGSizeMake(CGRectGetMaxX(sepLabelArr.lastObject.frame), selfHeight<labelHeight?labelHeight:selfHeight)};
  137. // center point detect
  138. if (self.centerPointPriority && changedFrame.size.width != self.frame.size.width && has_centerPoint_detect) {
  139. changedFrame = CGRectOffset(changedFrame, (self.frame.size.width - changedFrame.size.width)/2, 0);
  140. }
  141. if (self.centerPointPriority && !has_centerPoint_detect) {
  142. changedFrame = CGRectOffset(changedFrame, - changedFrame.size.width/2, - changedFrame.size.height/2);
  143. has_centerPoint_detect = YES;
  144. }
  145. self.frame = changedFrame;
  146. handler([sepStrArr copy],[sepLabelArr copy]);
  147. }
  148. // set
  149. - (void)setTextColor:(UIColor *)textColor{
  150. if (!is_initializeFromXib) {
  151. self.sp_textColor = textColor;
  152. }
  153. if (!self.isCommonLabel) {
  154. [super setTextColor:[UIColor clearColor]];
  155. }else{
  156. [super setTextColor:textColor];
  157. }
  158. }
  159. - (void)setIsCommonLabel:(BOOL)isCommonLabel{
  160. _isCommonLabel = isCommonLabel;
  161. [super setTextColor:isCommonLabel?self.sp_textColor:[UIColor clearColor]];
  162. }
  163. - (void)setText:(NSString *)text{
  164. [super setText:text];
  165. if (self.textColor == [UIColor clearColor] && self.isCommonLabel) {
  166. self.textColor = self.sp_textColor;
  167. }
  168. if ([self validateValueIsTotalNumber:text]) {
  169. _targetNumber = text.integerValue;
  170. }
  171. if (!self.isCommonLabel) {
  172. [self operateWithTargetString:text];
  173. }
  174. }
  175. - (void)setTargetNumber:(NSInteger)targetNumber{
  176. if (self.isCommonLabel) {
  177. return;
  178. }
  179. _targetNumber = targetNumber;
  180. [self setText:[NSString stringWithFormat:@"%ld",targetNumber]];
  181. }
  182. - (BOOL)validateValueIsTotalNumber:(NSString *)text{
  183. NSString *reg = @"^-?\\d*$";
  184. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", reg];
  185. return [predicate evaluateWithObject:text];
  186. }
  187. @end