Skip to content

Commit

Permalink
Merge pull request #337 from airbnb/btw/keyframeFix
Browse files Browse the repository at this point in the history
Fix for altering single keyframes
  • Loading branch information
buba447 committed Aug 3, 2017
2 parents 173e6d7 + e5be5e8 commit f26b778
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lottie-ios/Classes/AnimatableProperties/LOTKeyframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithKeyframe:(NSDictionary *)keyframe;
- (instancetype)initWithValue:(id)value;
- (void)remapValueWithBlock:(CGFloat (^)(CGFloat inValue))remapBlock;
- (void)setData:(id)data;
- (LOTKeyframe *)copyWithData:(id)data;

@property (nonatomic, readonly) NSNumber *keyframeTime;
@property (nonatomic, readonly) BOOL isHold;
Expand Down
27 changes: 23 additions & 4 deletions lottie-ios/Classes/AnimatableProperties/LOTKeyframe.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,35 @@ - (instancetype)initWithValue:(id)value {
return self;
}

- (void)remapValueWithBlock:(CGFloat (^)(CGFloat inValue))remapBlock {
_floatValue = remapBlock(_floatValue);
_pointValue = CGPointMake(remapBlock(_pointValue.x), remapBlock(_pointValue.y));
_sizeValue = CGSizeMake(remapBlock(_sizeValue.width), remapBlock(_sizeValue.height));
- (instancetype)initWithLOTKeyframe:(LOTKeyframe *)keyframe {
self = [super init];
if (self) {
_keyframeTime = [keyframe.keyframeTime copy];
_inTangent = keyframe.inTangent;
_outTangent = keyframe.outTangent;
_spatialInTangent = keyframe.spatialInTangent;
_spatialOutTangent = keyframe.spatialOutTangent;
_isHold = keyframe.isHold;
}
return self;
}

- (LOTKeyframe *)copyWithData:(id)data {
LOTKeyframe *newFrame = [[LOTKeyframe alloc] initWithLOTKeyframe:self];
[newFrame setData:data];
return newFrame;
}

- (void)setData:(id)data {
[self setupOutputWithData:data];
}

- (void)remapValueWithBlock:(CGFloat (^)(CGFloat inValue))remapBlock {
_floatValue = remapBlock(_floatValue);
_pointValue = CGPointMake(remapBlock(_pointValue.x), remapBlock(_pointValue.y));
_sizeValue = CGSizeMake(remapBlock(_sizeValue.width), remapBlock(_sizeValue.height));
}

- (void)setupOutputWithData:(id)data {
if ([data isKindOfClass:[NSNumber class]]) {
_floatValue = [(NSNumber*)data floatValue];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,20 @@ - (BOOL)setValue:(id)value atFrame:(NSNumber *)frame {
[self updateKeyframeSpanForFrame:frame];
if (frame.floatValue == self.leadingKeyframe.keyframeTime.floatValue) {
// Is leading frame, replace
[self.leadingKeyframe setData:data];
LOTKeyframe *newKeyframe = [self.leadingKeyframe copyWithData:data];
NSMutableArray *keyframes = [NSMutableArray arrayWithArray:_keyframes];
NSUInteger idx = [keyframes indexOfObject:self.leadingKeyframe];
[keyframes replaceObjectAtIndex:idx withObject:newKeyframe];
self.leadingKeyframe = newKeyframe;
_keyframes = keyframes;
} else if (frame.floatValue == self.trailingKeyframe.keyframeTime.floatValue) {
// Is trailing frame
[self.trailingKeyframe setData:data];
LOTKeyframe *newKeyframe = [self.trailingKeyframe copyWithData:data];
NSMutableArray *keyframes = [NSMutableArray arrayWithArray:_keyframes];
NSUInteger idx = [keyframes indexOfObject:self.trailingKeyframe];
[keyframes replaceObjectAtIndex:idx withObject:newKeyframe];
self.trailingKeyframe = newKeyframe;
_keyframes = keyframes;
} else {
// Is between leading and trailing. Either can be nil.
// For now added keyframes will default to linear interpolation.
Expand Down

0 comments on commit f26b778

Please sign in to comment.