Skip to content

Instantly share code, notes, and snippets.

@yulingtianxia
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yulingtianxia/2db8943bddb8baffc83c to your computer and use it in GitHub Desktop.
Save yulingtianxia/2db8943bddb8baffc83c to your computer and use it in GitHub Desktop.
一个可以显示数字的类,可以根据数字大小自动控制字号,并在数字变化时建立插值,产生指数渐变效果
#import <UIKit/UIKit.h>
@interface Credit : UILabel
-(void)autochangeFontsize:(double) number;
-(void)changeFromNumber:(double) originalnumber toNumber:(double) newnumber withAnimationTime:(NSTimeInterval)timeSpan;
@end
#import "Credit.h"
#define IncreaseSpeed 100
@implementation Credit
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
self.textAlignment = NSTextAlignmentCenter;
[self setFont:[UIFont fontWithName:@"FZLanTingHei-UL-GBK" size:60.0]];
[self setTextColor:[UIColor whiteColor]];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[super drawRect:rect];
[self setFont:[UIFont fontWithName:@"FZLanTingHei-UL-GBK" size:30.0]];
}
-(void)autochangeFontsize:(double) number{
if (number<100000) {
[self setFont:[UIFont fontWithName:@"FZLanTingHei-UL-GBK" size:60.0]];
}
else if (number<1000000){
[self setFont:[UIFont fontWithName:@"FZLanTingHei-UL-GBK" size:50.0]];
}
else if (number<10000000){
[self setFont:[UIFont fontWithName:@"FZLanTingHei-UL-GBK" size:40.0]];
}
}
-(void)changeFromNumber:(double) originalnumber toNumber:(double) newnumber withAnimationTime:(NSTimeInterval)timeSpan{
[UIView animateWithDuration:timeSpan delay:3 options:UIViewAnimationOptionTransitionNone animations:^{
NSString *currencyStr = [NSNumberFormatter localizedStringFromNumber:[NSNumber numberWithDouble: originalnumber] numberStyle:NSNumberFormatterCurrencyStyle];
currencyStr = [currencyStr substringWithRange:NSMakeRange(1, currencyStr.length-2)];
if ([[currencyStr substringFromIndex:currencyStr.length-1] isEqualToString:@"0"]) {
currencyStr =[currencyStr substringToIndex:currencyStr.length-2];
}
[self autochangeFontsize:originalnumber];
self.text = currencyStr;
} completion:^(BOOL finished) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeSpan * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if (labs((newnumber-originalnumber)/AnimationSpeed)<1) {
[self changeFromNumber:newnumber toNumber:newnumber withAnimationTime:timeSpan];
}
// else if (originalnumber+(newnumber-originalnumber)/IncreaseSpeed<=newnumber) {
else if(labs((newnumber-originalnumber)/AnimationSpeed)<labs(newnumber-originalnumber)){
[self changeFromNumber:originalnumber+(newnumber-originalnumber)/AnimationSpeed toNumber:newnumber withAnimationTime:timeSpan];
}
else if(originalnumber==newnumber){
// [self changeFromNumber:newnumber toNumber:newnumber withAnimationTime:timeSpan];
}
});
}];
}
@end
@yulingtianxia
Copy link
Author

-(void)changeFromNumber:toNumber:withAnimationTime:方法可以传入初试数字和目标数字,并设定插值的时间间隔,数字变化速度是匀速,但是变化的大小是Easeout的,可以通过更改IncreaseSpeed来调节变化速度。使用递归方法实现,效率有待优化。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment