Skip to content

Commit

Permalink
add bounce animation
Browse files Browse the repository at this point in the history
  • Loading branch information
BESLAN TULAROV authored and BESLAN TULAROV committed Feb 12, 2017
1 parent 1aa3d51 commit b72eb7f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
4 changes: 3 additions & 1 deletion UICountingLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ typedef NS_ENUM(NSInteger, UILabelCountingMethod) {
UILabelCountingMethodEaseInOut,
UILabelCountingMethodEaseIn,
UILabelCountingMethodEaseOut,
UILabelCountingMethodLinear
UILabelCountingMethodLinear,
UILabelCountingMethodEaseInBounce,
UILabelCountingMethodEaseOutBounce
};

typedef NSString* (^UICountingLabelFormatBlock)(CGFloat value);
Expand Down
58 changes: 58 additions & 0 deletions UICountingLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ @interface UILabelCounterEaseInOut : NSObject<UILabelCounter>

@end

@interface UILabelCounterEaseInBounce : NSObject<UILabelCounter>

@end

@interface UILabelCounterEaseOutBounce : NSObject<UILabelCounter>

@end

@implementation UILabelCounterLinear

-(CGFloat)update:(CGFloat)t
Expand Down Expand Up @@ -73,6 +81,50 @@ -(CGFloat) update: (CGFloat) t

@end

@implementation UILabelCounterEaseInBounce

-(CGFloat) update: (CGFloat) t {

if (t < 4.0 / 11.0) {
return 1.0 - (powf(11.0 / 4.0, 2) * powf(t, 2)) - t;
}

if (t < 8.0 / 11.0) {
return 1.0 - (3.0 / 4.0 + powf(11.0 / 4.0, 2) * powf(t - 6.0 / 11.0, 2)) - t;
}

if (t < 10.0 / 11.0) {
return 1.0 - (15.0 /16.0 + powf(11.0 / 4.0, 2) * powf(t - 9.0 / 11.0, 2)) - t;
}

return 1.0 - (63.0 / 64.0 + powf(11.0 / 4.0, 2) * powf(t - 21.0 / 22.0, 2)) - t;

}

@end

@implementation UILabelCounterEaseOutBounce

-(CGFloat) update: (CGFloat) t {

if (t < 4.0 / 11.0) {
return powf(11.0 / 4.0, 2) * powf(t, 2);
}

if (t < 8.0 / 11.0) {
return 3.0 / 4.0 + powf(11.0 / 4.0, 2) * powf(t - 6.0 / 11.0, 2);
}

if (t < 10.0 / 11.0) {
return 15.0 /16.0 + powf(11.0 / 4.0, 2) * powf(t - 9.0 / 11.0, 2);
}

return 63.0 / 64.0 + powf(11.0 / 4.0, 2) * powf(t - 21.0 / 22.0, 2);

}

@end

#pragma mark - UICountingLabel

@interface UICountingLabel ()
Expand Down Expand Up @@ -138,6 +190,12 @@ -(void)countFrom:(CGFloat)startValue to:(CGFloat)endValue withDuration:(NSTimeIn
case UILabelCountingMethodEaseInOut:
self.counter = [[UILabelCounterEaseInOut alloc] init];
break;
case UILabelCountingMethodEaseOutBounce:
self.counter = [[UILabelCounterEaseOutBounce alloc] init];
break;
case UILabelCountingMethodEaseInBounce:
self.counter = [[UILabelCounterEaseInBounce alloc] init];
break;
}

CADisplayLink *timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateValue:)];
Expand Down

0 comments on commit b72eb7f

Please sign in to comment.