Skip to content

Commit a21eec7

Browse files
committed
added variadic methods for creating keyframes
1 parent 4c2cef8 commit a21eec7

5 files changed

+131
-1
lines changed

JazzHandsDemo/JazzHandsDemo/IFTTTJazzHandsViewController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#define NUMBER_OF_PAGES 4
1212

13-
#define timeForPage(page) (self.view.frame.size.width * (page - 1))
13+
#define timeForPage(page) (NSInteger)(self.view.frame.size.width * (page - 1))
1414
#define xForPage(page) timeForPage(page)
1515

1616
@interface IFTTTJazzHandsViewController ()

src/IFTTTAnimation.h

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
- (id)initWithView:(UIView *)view;
1919

2020
- (void)animate:(NSInteger)time;
21+
22+
- (void)addKeyFrames:(NSArray *)keyFrames;
2123
- (void)addKeyFrame:(IFTTTAnimationKeyFrame *)keyFrame;
2224

2325
- (IFTTTAnimationFrame *)animationFrameForTime:(NSInteger)time;

src/IFTTTAnimation.m

+7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ - (id)initWithView:(UIView *)view
4343
return self;
4444
}
4545

46+
- (void)addKeyFrames:(NSArray *)keyFrames
47+
{
48+
for (IFTTTAnimationKeyFrame *keyFrame in keyFrames) {
49+
[self addKeyFrame:keyFrame];
50+
}
51+
}
52+
4653
- (void)addKeyFrame:(IFTTTAnimationKeyFrame *)keyFrame
4754
{
4855
if (self.keyFrames.count == 0) {

src/IFTTTAnimationKeyFrame.h

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010

1111
@interface IFTTTAnimationKeyFrame : IFTTTAnimationFrame
1212

13+
// These variadic methods require arguments to be of the form
14+
//
15+
// n, time_1, value_1, time_2, value_2, ..., time_n, value_n,
16+
//
17+
// where n is the number of time-value pairs. That is, the number of pairs must be the
18+
// first argument, then the method will take care of unpacking the rest of the times and
19+
// values into keyframe objects.
20+
//
21+
+ (NSArray *)keyFramesWithTimesAndAlphas:(NSInteger)pairCount,...;
22+
+ (NSArray *)keyFramesWithTimesAndFrames:(NSInteger)pairCount,...;
23+
+ (NSArray *)keyFramesWithTimesAndHiddens:(NSInteger)pairCount,...;
24+
+ (NSArray *)keyFramesWithTimesAndColors:(NSInteger)pairCount,...;
25+
1326
+ (instancetype)keyFrameWithTime:(NSInteger)time andAlpha:(CGFloat)alpha;
1427
+ (instancetype)keyFrameWithTime:(NSInteger)time andFrame:(CGRect)frame;
1528
+ (instancetype)keyFrameWithTime:(NSInteger)time andHidden:(BOOL)hidden;

src/IFTTTAnimationKeyFrame.m

+108
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,114 @@
1010

1111
@implementation IFTTTAnimationKeyFrame
1212

13+
+ (NSArray *)keyFramesWithTimesAndAlphas:(NSInteger)pairCount,...
14+
{
15+
va_list argumentList;
16+
NSInteger time;
17+
CGFloat alpha;
18+
if (pairCount > 0) {
19+
NSMutableArray *keyFrames = [NSMutableArray arrayWithCapacity:pairCount];
20+
21+
va_start(argumentList, pairCount);
22+
23+
for (int i=0; i<pairCount; i++) {
24+
time = va_arg(argumentList, NSInteger);
25+
alpha = va_arg(argumentList, double); // use double to suppress a va_arg conversion warning
26+
IFTTTAnimationKeyFrame *keyFrame = [IFTTTAnimationKeyFrame keyFrameWithTime: time
27+
andAlpha: alpha];
28+
[keyFrames addObject:keyFrame];
29+
}
30+
31+
va_end(argumentList);
32+
33+
return [NSArray arrayWithArray:keyFrames];
34+
}
35+
else {
36+
return nil;
37+
}
38+
}
39+
40+
+ (NSArray *)keyFramesWithTimesAndFrames:(NSInteger)pairCount,...
41+
{
42+
va_list argumentList;
43+
NSInteger time;
44+
CGRect frame;
45+
if (pairCount > 0) {
46+
NSMutableArray *keyFrames = [NSMutableArray arrayWithCapacity:pairCount];
47+
48+
va_start(argumentList, pairCount);
49+
50+
for (int i=0; i<pairCount; i++) {
51+
time = va_arg(argumentList, NSInteger);
52+
frame = va_arg(argumentList, CGRect);
53+
IFTTTAnimationKeyFrame *keyFrame = [IFTTTAnimationKeyFrame keyFrameWithTime: time
54+
andFrame: frame];
55+
[keyFrames addObject:keyFrame];
56+
}
57+
58+
va_end(argumentList);
59+
60+
return [NSArray arrayWithArray:keyFrames];
61+
}
62+
else {
63+
return nil;
64+
}
65+
}
66+
67+
+ (NSArray *)keyFramesWithTimesAndHiddens:(NSInteger)pairCount,...
68+
{
69+
va_list argumentList;
70+
NSInteger time;
71+
BOOL hidden;
72+
if (pairCount > 0) {
73+
NSMutableArray *keyFrames = [NSMutableArray arrayWithCapacity:pairCount];
74+
75+
va_start(argumentList, pairCount);
76+
77+
for (int i=0; i<pairCount; i++) {
78+
time = va_arg(argumentList, NSInteger);
79+
hidden = va_arg(argumentList, int); // use int to suppress a va_arg conversion warning
80+
IFTTTAnimationKeyFrame *keyFrame = [IFTTTAnimationKeyFrame keyFrameWithTime: time
81+
andHidden: hidden];
82+
[keyFrames addObject:keyFrame];
83+
}
84+
85+
va_end(argumentList);
86+
87+
return [NSArray arrayWithArray:keyFrames];
88+
}
89+
else {
90+
return nil;
91+
}
92+
}
93+
94+
+ (NSArray *)keyFramesWithTimesAndColors:(NSInteger)pairCount,...
95+
{
96+
va_list argumentList;
97+
NSInteger time;
98+
UIColor *color;
99+
if (pairCount > 0) {
100+
NSMutableArray *keyFrames = [NSMutableArray arrayWithCapacity:pairCount];
101+
102+
va_start(argumentList, pairCount);
103+
104+
for (int i=0; i<pairCount; i++) {
105+
time = va_arg(argumentList, NSInteger);
106+
color = va_arg(argumentList, id);
107+
IFTTTAnimationKeyFrame *keyFrame = [IFTTTAnimationKeyFrame keyFrameWithTime: time
108+
andColor: color];
109+
[keyFrames addObject:keyFrame];
110+
}
111+
112+
va_end(argumentList);
113+
114+
return [NSArray arrayWithArray:keyFrames];
115+
}
116+
else {
117+
return nil;
118+
}
119+
}
120+
13121
+ (instancetype)keyFrameWithTime:(NSInteger)time andAlpha:(CGFloat)alpha
14122
{
15123
IFTTTAnimationKeyFrame *keyFrame = [[[self class] alloc] initWithTime: time

0 commit comments

Comments
 (0)