Created
December 22, 2011 21:45
-
-
Save cieslak/1511997 to your computer and use it in GitHub Desktop.
Generic Bounce CAKeyfameAnimation
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
// | |
// EPFBounceAnimation.h | |
// | |
// Created by Chris Cieslak on 12/22/11. | |
// | |
#import <QuartzCore/QuartzCore.h> | |
@interface EPFBounceAnimation : CAKeyframeAnimation | |
+ (id) animationWithKeyPath:(NSString *)path startValue:(double)startValue endValue:(double)endValue steps:(NSUInteger)steps; | |
@end |
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
// | |
// EPFBounceAnimation.m | |
// | |
// Created by Chris Cieslak on 12/22/11. | |
// Derived from http://cocoawithlove.com/2008/09/parametric-acceleration-curves-in-core.html | |
// Created by Matt Gallagher on 7/09/08. | |
// Copyright 2008 Matt Gallagher. All rights reserved. | |
// | |
// Permission is given to use this source code file without charge in any | |
// project, commercial or otherwise, entirely at your risk, with the condition | |
// that any redistribution (in part or whole) of source code must retain | |
// this copyright and permission notice. Attribution in compiled projects is | |
// appreciated but not required. | |
// | |
#import "EPFBounceAnimation.h" | |
@interface EPFBounceAnimation() | |
- (double)calculatedValueForStep:(double)step; | |
@end | |
@implementation EPFBounceAnimation | |
+ (id) animationWithKeyPath:(NSString *)path startValue:(double)startValue endValue:(double)endValue steps:(NSUInteger)steps { | |
EPFBounceAnimation *animation = [EPFBounceAnimation animationWithKeyPath:path]; | |
NSUInteger count = steps + 2; | |
NSMutableArray *valueArray = [NSMutableArray arrayWithCapacity:count]; | |
double progress = 0.0; | |
double increment = 1.0 / (double)(count - 1); | |
for (int i = 0; i < count; i++) { | |
double value = startValue + [animation calculatedValueForStep:progress] * (endValue - startValue); | |
[valueArray addObject:[NSNumber numberWithDouble:value]]; | |
progress += increment; | |
} | |
animation.values = valueArray; | |
return animation; | |
} | |
- (double)calculatedValueForStep:(double)step { | |
double omega = 20.0; | |
double zeta = 0.53; | |
double beta = sqrt(1 - zeta * zeta); | |
double phi = atan(beta / zeta); | |
double result = 1.0 + -1.0 / beta * exp(-zeta * omega * step) * sin(beta * omega * step + phi); | |
return result; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment