Skip to content

Overridable default keyframe count, and factory methods allowing keyframe count to be set #1

Merged
merged 1 commit into from Mar 28, 2012
View
9 AHEasing/CAKeyframeAnimation+AHEasing.h
@@ -16,12 +16,21 @@
@interface CAKeyframeAnimation (AHEasing)
// Factory method to create a keyframe animation for animating a scalar value
++ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromValue:(CGFloat)fromValue toValue:(CGFloat)toValue keyframeCount:(size_t)keyframeCount;
+
+// Factory method to create a keyframe animation for animating a scalar value, with keyFrameCount set to AHEasingDefaultKeyframeCount
+ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromValue:(CGFloat)fromValue toValue:(CGFloat)toValue;
// Factory method to create a keyframe animation for animating between two points
++ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint keyframeCount:(size_t)keyframeCount;
+
+// Factory method to create a keyframe animation for animating between two points, with keyFrameCount set to AHEasingDefaultKeyframeCount
+ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromPoint:(CGPoint)fromValue toPoint:(CGPoint)toValue;
// Factory method to create a keyframe animation for animating between two sizes
++ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromSize:(CGSize)fromSize toSize:(CGSize)toSize keyframeCount:(size_t)keyframeCount;
+
+// Factory method to create a keyframe animation for animating between two sizes, with keyFrameCount set to AHEasingDefaultKeyframeCount
+ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromSize:(CGSize)fromValue toSize:(CGSize)toValue;
@end
View
45 AHEasing/CAKeyframeAnimation+AHEasing.m
@@ -12,18 +12,22 @@
#import "CAKeyframeAnimation+AHEasing.h"
+#if !defined(AHEasingDefaultKeyframeCount)
+
// The larger this number, the smoother the animation
-#define AHEasingKeyframeCount 60
+#define AHEasingDefaultKeyframeCount 60
+
+#endif
@implementation CAKeyframeAnimation (AHEasing)
-+ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromValue:(CGFloat)fromValue toValue:(CGFloat)toValue
++ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromValue:(CGFloat)fromValue toValue:(CGFloat)toValue keyframeCount:(size_t)keyframeCount
{
- NSMutableArray *values = [NSMutableArray arrayWithCapacity:AHEasingKeyframeCount];
+ NSMutableArray *values = [NSMutableArray arrayWithCapacity:keyframeCount];
CGFloat t = 0.0;
- CGFloat dt = 1.0 / (AHEasingKeyframeCount - 1);
- for(size_t frame = 0; frame < AHEasingKeyframeCount; ++frame, t += dt)
+ CGFloat dt = 1.0 / (keyframeCount - 1);
+ for(size_t frame = 0; frame < keyframeCount; ++frame, t += dt)
{
CGFloat value = fromValue + function(t) * (toValue - fromValue);
[values addObject:[NSNumber numberWithFloat:value]];
@@ -34,13 +38,18 @@ + (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function
return animation;
}
-+ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint
++ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromValue:(CGFloat)fromValue toValue:(CGFloat)toValue
{
- NSMutableArray *values = [NSMutableArray arrayWithCapacity:AHEasingKeyframeCount];
+ return [self animationWithKeyPath:path function:function fromValue:fromValue toValue:toValue keyframeCount:AHEasingDefaultKeyframeCount];
+}
+
++ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint keyframeCount:(size_t)keyframeCount
+{
+ NSMutableArray *values = [NSMutableArray arrayWithCapacity:keyframeCount];
CGFloat t = 0.0;
- CGFloat dt = 1.0 / (AHEasingKeyframeCount - 1);
- for(size_t frame = 0; frame < AHEasingKeyframeCount; ++frame, t += dt)
+ CGFloat dt = 1.0 / (keyframeCount - 1);
+ for(size_t frame = 0; frame < keyframeCount; ++frame, t += dt)
{
CGFloat x = fromPoint.x + function(t) * (toPoint.x - fromPoint.x);
CGFloat y = fromPoint.y + function(t) * (toPoint.y - fromPoint.y);
@@ -52,13 +61,18 @@ + (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function
return animation;
}
-+ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromSize:(CGSize)fromSize toSize:(CGSize)toSize
++ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint
+{
+ return [self animationWithKeyPath:path function:function fromPoint:fromPoint toPoint:toPoint keyframeCount:AHEasingDefaultKeyframeCount];
+}
+
++ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromSize:(CGSize)fromSize toSize:(CGSize)toSize keyframeCount:(size_t)keyframeCount
{
- NSMutableArray *values = [NSMutableArray arrayWithCapacity:AHEasingKeyframeCount];
+ NSMutableArray *values = [NSMutableArray arrayWithCapacity:keyframeCount];
CGFloat t = 0.0;
- CGFloat dt = 1.0 / (AHEasingKeyframeCount - 1);
- for(size_t frame = 0; frame < AHEasingKeyframeCount; ++frame, t += dt)
+ CGFloat dt = 1.0 / (keyframeCount - 1);
+ for(size_t frame = 0; frame < keyframeCount; ++frame, t += dt)
{
CGFloat w = fromSize.width + function(t) * (toSize.width - fromSize.width);
CGFloat h = fromSize.height + function(t) * (toSize.height - fromSize.height);
@@ -70,4 +84,9 @@ + (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function
return animation;
}
++ (id)animationWithKeyPath:(NSString *)path function:(AHEasingFunction)function fromSize:(CGSize)fromSize toSize:(CGSize)toSize
+{
+ return [self animationWithKeyPath:path function:function fromSize:fromSize toSize:toSize keyframeCount:AHEasingDefaultKeyframeCount];
+}
+
@end
Something went wrong with that request. Please try again.