@@ -17,18 +17,12 @@ @interface IFTTTKeyframe : NSObject
17
17
@property (nonatomic , strong ) id <IFTTTInterpolatable> value;
18
18
@property (nonatomic , copy ) IFTTTEasingFunction easingFunction;
19
19
20
- - (instancetype )initWithTime : (CGFloat )time value : (id )value ;
21
20
- (instancetype )initWithTime : (CGFloat )time value : (id )value easingFunction : (IFTTTEasingFunction)easingFunction ;
22
21
23
22
@end
24
23
25
24
@implementation IFTTTKeyframe
26
25
27
- - (instancetype )initWithTime : (CGFloat )time value : (id )value
28
- {
29
- return [self initWithTime: time value: value easingFunction: IFTTTEasingFunctionLinear];
30
- }
31
-
32
26
- (instancetype )initWithTime : (CGFloat )time value : (id )value easingFunction : (IFTTTEasingFunction)easingFunction
33
27
{
34
28
if ((self = [super init ])) {
@@ -66,15 +60,15 @@ - (BOOL)isEmpty
66
60
67
61
- (void )setValue : (id <IFTTTInterpolatable>)value atTime : (CGFloat )time
68
62
{
69
- NSUInteger indexAfter = [self indexOfKeyframeAfterTime: time ];
70
- IFTTTKeyframe *newKeyframe = [[IFTTTKeyframe alloc ] initWithTime: time value: value];
71
- [self .keyframes insertObject: newKeyframe atIndex: indexAfter];
63
+ [self setValue: value atTime: time withEasingFunction: IFTTTEasingFunctionLinear];
72
64
}
73
65
74
66
- (void )setValue : (id <IFTTTInterpolatable>)value atTime : (CGFloat )time withEasingFunction : (IFTTTEasingFunction)easingFunction
75
67
{
76
- NSUInteger indexAfter = [self indexOfKeyframeAfterTime: time ];
68
+ NSAssert ([self canInterpolateNewValue: value], @" New value must have the same interpolatable type as existing keyframe values." );
69
+
77
70
IFTTTKeyframe *newKeyframe = [[IFTTTKeyframe alloc ] initWithTime: time value: value easingFunction: easingFunction];
71
+ NSUInteger indexAfter = [self indexOfKeyframeAfterTime: time ];
78
72
[self .keyframes insertObject: newKeyframe atIndex: indexAfter];
79
73
}
80
74
@@ -89,14 +83,7 @@ - (void)setValue:(id<IFTTTInterpolatable>)value atTime:(CGFloat)time withEasingF
89
83
IFTTTKeyframe *keyframeBefore = (IFTTTKeyframe *)self.keyframes [indexAfter - 1 ];
90
84
IFTTTKeyframe *keyframeAfter = (IFTTTKeyframe *)self.keyframes [indexAfter];
91
85
CGFloat progress = [self progressFromTime: keyframeBefore.time toTime: keyframeAfter.time atTime: time withEasingFunction: keyframeBefore.easingFunction];
92
- if ([keyframeBefore.value respondsToSelector: @selector (interpolateTo:withProgress: )]
93
- && ([keyframeAfter.value isKindOfClass: [keyframeBefore.value class ]]
94
- || ([keyframeBefore.value isKindOfClass: [UIColor class ]]
95
- && [keyframeAfter.value isKindOfClass: [UIColor class ]]))) {
96
- value = [keyframeBefore.value interpolateTo: keyframeAfter.value withProgress: progress];
97
- } else {
98
- value = keyframeBefore.value ;
99
- }
86
+ value = [keyframeBefore.value interpolateTo: keyframeAfter.value withProgress: progress];
100
87
} else {
101
88
value = ((IFTTTKeyframe *)self.keyframes .lastObject ).value ;
102
89
}
@@ -122,4 +109,18 @@ - (CGFloat)progressFromTime:(CGFloat)fromTime toTime:(CGFloat)toTime atTime:(CGF
122
109
return easingFunction (timeElapsed / duration);
123
110
}
124
111
112
+ - (BOOL )canInterpolateNewValue : (id )newValue
113
+ {
114
+ if (self.keyframes .count == 0 ) {
115
+ return YES ;
116
+ }
117
+
118
+ IFTTTKeyframe *existingKeyframe = (IFTTTKeyframe *)self.keyframes .firstObject ;
119
+
120
+ return ([newValue respondsToSelector: @selector (interpolateTo:withProgress: )]
121
+ && ([newValue isKindOfClass: [existingKeyframe.value class ]]
122
+ || ([existingKeyframe.value isKindOfClass: [UIColor class ]]
123
+ && [newValue isKindOfClass: [UIColor class ]])));
124
+ }
125
+
125
126
@end
0 commit comments