Last active
August 29, 2015 14:10
-
-
Save HamGuy/1ccc4ddee60024ba561e to your computer and use it in GitHub Desktop.
余额宝数字动画效果,基于https://github.com/PigRiver/NumberJumpDemo 简单修改
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CATextLayer+NumberJump.m | |
// HGCodeScanner | |
// | |
// Created by HamGuy on 12/8/14. | |
// Copyright (c) 2014 HamGuy. All rights reserved. | |
// | |
#import "CATextLayer+NumberJump.h" | |
#import "NJDBezierCurve.h" | |
#define kPointsNumber 100 // 即数字跳100次 | |
@implementation CATextLayer (NumberJump) | |
NSMutableArray *numberPoints;//记录每次textLayer更改值的间隔时间及输出值。 | |
CGFloat lastTime; | |
NSInteger indexNumber; | |
Point2D startPoint; | |
Point2D controlPoint1; | |
Point2D controlPoint2; | |
Point2D endPoint; | |
CGFloat _duration; | |
CGFloat _startNumber; | |
CGFloat _endNumber; | |
- (void)cleanUpValue { | |
lastTime = 0; | |
indexNumber = 0; | |
self.string = [NSString stringWithFormat:@"%.0f",_startNumber]; | |
} | |
- (void)jumpNumberWithDuration:(CGFloat)duration | |
fromNumber:(CGFloat)startNumber | |
toNumber:(CGFloat)endNumber { | |
_duration = duration; | |
_startNumber = startNumber; | |
_endNumber = endNumber; | |
[self cleanUpValue]; | |
[self initPoints]; | |
[self changeNumberBySelector]; | |
} | |
- (void)initPoints { | |
// 贝塞尔曲线 | |
[self initBezierPoints]; | |
Point2D bezierCurvePoints[4] = {startPoint, controlPoint1, controlPoint2, endPoint}; | |
numberPoints = [[NSMutableArray alloc] init]; | |
CGFloat dt; | |
dt = 1.0 / (kPointsNumber - 1); | |
for (NSInteger i = 0; i < kPointsNumber; i++) { | |
Point2D point = PointOnCubicBezier(bezierCurvePoints, i*dt); | |
CGFloat durationTime = point.x * _duration; | |
CGFloat value = point.y * (_endNumber - _startNumber) + _startNumber; | |
[numberPoints addObject:[NSArray arrayWithObjects:[NSNumber numberWithFloat:durationTime], [NSNumber numberWithFloat:value], nil]]; | |
} | |
} | |
- (void)initBezierPoints { | |
// 可到http://cubic-bezier.com自定义贝塞尔曲线 | |
startPoint.x = 0; | |
startPoint.y = 0; | |
controlPoint1.x = 0.25; | |
controlPoint1.y = 0.1; | |
controlPoint2.x = 0.25; | |
controlPoint2.y = 1; | |
endPoint.x = 1; | |
endPoint.y = 1; | |
} | |
- (void)changeNumberBySelector { | |
if (indexNumber >= kPointsNumber) { | |
self.string = [NSString stringWithFormat:@"%.0f",_endNumber]; | |
return; | |
} else { | |
NSArray *pointValues = [numberPoints objectAtIndex:indexNumber]; | |
indexNumber++; | |
CGFloat value = [(NSNumber *)[pointValues objectAtIndex:1] intValue]; | |
CGFloat currentTime = [(NSNumber *)[pointValues objectAtIndex:0] floatValue]; | |
CGFloat timeDuration = currentTime - lastTime; | |
lastTime = currentTime; | |
self.string = [NSString stringWithFormat:@"%.0f",value]; | |
[self performSelector:@selector(changeNumberBySelector) withObject:nil afterDelay:timeDuration]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment