// // SPScrollNumLabel.m // SPScrollNumLabel // // Created by Tree on 2017/11/21. // Copyright © 2017年 Tr2e. All rights reserved. // #import "SPScrollNumLabel.h" @interface SPScrollNumLabel (){ BOOL has_centerPoint_detect; BOOL is_initializeFromXib; } @property (nonatomic, strong) UIColor *sp_textColor;// default is black @property (nonatomic, copy) NSArray *sepNumArr; @property (nonatomic, copy) NSArray *sepLabelArr; @end @implementation SPScrollNumLabel // initialize - (instancetype)initWithCoder:(NSCoder *)aDecoder{ if (self = [super initWithCoder:aDecoder]) { [self initailizeConfig]; } return self; } - (void)layoutSubviews{ [super layoutSubviews]; } - (void)willMoveToWindow:(UIWindow *)newWindow{ [super willMoveToWindow:newWindow]; if (!_targetNumber && !self.isCommonLabel) { self.targetNumber = 0; } } - (void)initailizeConfig{ is_initializeFromXib = YES; if (!self.isCommonLabel) { self.sp_textColor = self.textColor; self.textColor = [UIColor clearColor]; } } // function - (void)increaseNumber:(NSInteger)increasedNum{ self.targetNumber += increasedNum; } - (void)decreaseNumber:(NSInteger)decreasedNum{ self.targetNumber -= decreasedNum; } - (void)operateWithTargetString:(NSString *)string{ __weak typeof(self) ws = self; [self seprateStringToSingleWithTarget:string operateHandler:^(NSMutableArray *sepStrArr, NSMutableArray *sepLabelArr) { [ws showScrollAnimiationWithSepStrArr:sepStrArr sepLableArr:sepLabelArr]; }]; } - (void)showScrollAnimiationWithSepStrArr:(NSArray *)sepStrArr sepLableArr:(NSArray *)sepLabelArr{ NSInteger targetLength = sepLabelArr.count; NSInteger hisLength = self.sepLabelArr.count; for (NSInteger i = 0; i < targetLength; i ++) { NSInteger targetIndex = targetLength - i - 1; NSInteger hisTargetIndex = hisLength - i - 1; if (i < hisLength) { if (![sepStrArr[targetIndex] isEqualToString:self.sepNumArr[hisTargetIndex]]) { BOOL increase = sepStrArr[targetIndex].integerValue > self.sepNumArr[hisTargetIndex].integerValue; // his label UILabel *hisLabel = self.sepLabelArr[hisTargetIndex]; CGRect hisTargetFrame = CGRectOffset(hisLabel.frame, 0, (increase?-1:1)*hisLabel.frame.size.height); // new label UILabel *label = sepLabelArr[targetIndex]; CGRect frame = label.frame; CGRect targetFrame = CGRectOffset(frame, 0, (increase?1:-1)*label.frame.size.height); label.frame = targetFrame; label.alpha = 0; [UIView animateWithDuration:self.animateDuration?self.animateDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ hisLabel.frame = hisTargetFrame; label.frame = frame; hisLabel.alpha = 0; label.alpha = 1; } completion:^(BOOL finished) { [hisLabel removeFromSuperview]; }]; }else{ [self.sepLabelArr[hisTargetIndex] removeFromSuperview]; } }else{ UILabel *label = sepLabelArr[targetIndex]; CGRect frame = label.frame; label.alpha = 0; label.frame = CGRectOffset(frame, 0, -frame.size.height); [UIView animateWithDuration:self.animateDuration?self.animateDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:2 options:UIViewAnimationOptionCurveEaseOut animations:^{ label.frame = frame; label.alpha = 1; } completion:nil]; } } if (hisLength > targetLength) { for (int i = 0; i < hisLength - targetLength; i ++) { UILabel *label = self.sepLabelArr[i]; CGRect hisTargetFrame = CGRectOffset(label.frame, 0, -1*label.frame.size.height); [UIView animateWithDuration:self.animateDuration?self.animateDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ label.frame = hisTargetFrame; label.alpha = 0; } completion:^(BOOL finished) { [label removeFromSuperview]; }]; } } self.sepNumArr = sepStrArr; self.sepLabelArr = sepLabelArr; } // private operation - (UILabel *)createLabelWithString:(NSString *)numberStr{ UILabel *label = [[UILabel alloc] init]; label.font = self.font; label.textColor = self.sp_textColor?self.sp_textColor:self.textColor; label.textAlignment = NSTextAlignmentLeft; label.text = numberStr; [label sizeToFit]; return label; } - (void)seprateStringToSingleWithTarget:(NSString *)target operateHandler:(void (^) (NSMutableArray *sepStrArr,NSMutableArray *sepLabelArr)) handler{ NSMutableArray * sepStrArr = [[NSMutableArray alloc] initWithCapacity:target.length]; NSMutableArray * sepLabelArr = [[NSMutableArray alloc] initWithCapacity:sepStrArr.count]; for (NSInteger i = 0 ; i < target.length; i ++) { // str NSString *subStr = [target substringWithRange:NSMakeRange(i, 1)]; [sepStrArr addObject:subStr]; // label UILabel *label = [self createLabelWithString:subStr]; CGFloat labelHeight = label.frame.size.height; CGFloat selfHeight = self.frame.size.height; label.frame = (CGRect){CGPointMake(sepLabelArr.count?CGRectGetMaxX(sepLabelArr.lastObject.frame):0, (labelHeight < selfHeight ? (selfHeight - labelHeight)/2: 0)),label.frame.size}; [sepLabelArr addObject:label]; [self addSubview:label]; } CGFloat labelHeight = sepLabelArr.lastObject.frame.size.height; CGFloat selfHeight = self.frame.size.height; CGRect changedFrame = (CGRect){self.frame.origin,CGSizeMake(CGRectGetMaxX(sepLabelArr.lastObject.frame), selfHeight