Skip to content

Commit

Permalink
Merge pull request airbnb#245 from 3sidedcube/fix/setting_progress
Browse files Browse the repository at this point in the history
Fixes setting initial progress of Lottie animation
  • Loading branch information
buba447 committed Jun 7, 2017
2 parents 2968dba + 2363b3d commit 554320b
Showing 1 changed file with 58 additions and 37 deletions.
95 changes: 58 additions & 37 deletions lottie-ios/Classes/Private/LOTAnimationView.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ - (id)initWithDuration:(CGFloat)duration layer:(CALayer *)layer frameRate:(NSNum
_layer.beginTime = CACurrentMediaTime();

_previousLocalTime = -1.f;
[self setNeedsAnimationUpdate];

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
if (_animationIsPlaying) {
[self setAnimationIsPlaying:_animationIsPlaying];
} else {
[self setAnimatedProgress:_animatedProgress updateAnimation:false];
}
}];
}
return self;
}
Expand All @@ -62,7 +69,7 @@ - (void)updateAnimationLayerWithTimeOffset:(CFTimeInterval)timeOffset {
_layer.repeatCount = _loopAnimation ? HUGE_VALF : 0;
_layer.timeOffset = 0;
_layer.beginTime = 0;

if (speed == 0) {
_layer.timeOffset = timeOffset;
} else {
Expand All @@ -81,22 +88,25 @@ - (void)setNeedsAnimationUpdate {
if (_animationIsPlaying) {
[self setAnimationIsPlaying:_animationIsPlaying];
} else {
[self setAnimatedProgress:_animatedProgress];
[self setAnimatedProgress:_animatedProgress updateAnimation:true];
}
[self.layer setNeedsDisplay];
}];
}
}

#pragma mark -- Setters

- (void)setAnimationDoesLoop:(BOOL)loopAnimation {
- (void)setAnimationDoesLoop:(BOOL)loopAnimation updateAnimation:(BOOL)updateAnimation {
_loopAnimation = loopAnimation;
CFTimeInterval offset = [_layer convertTime:CACurrentMediaTime() fromLayer:nil];
__unused CFTimeInterval clock = CACurrentMediaTime();
[self updateAnimationLayerWithTimeOffset:offset];
if (updateAnimation) {
CFTimeInterval offset = [_layer convertTime:CACurrentMediaTime() fromLayer:nil];
__unused CFTimeInterval clock = CACurrentMediaTime();
[self updateAnimationLayerWithTimeOffset:offset];
}
}

- (void)setAnimationIsPlaying:(BOOL)animationIsPlaying {
- (void)setAnimationIsPlaying:(BOOL)animationIsPlaying {
_animationIsPlaying = animationIsPlaying;
CFTimeInterval offset = [_layer convertTime:CACurrentMediaTime() fromLayer:nil];
__unused CFTimeInterval clock = CACurrentMediaTime();
Expand All @@ -112,22 +122,29 @@ - (void)setAnimationIsPlaying:(BOOL)animationIsPlaying {
[self updateAnimationLayerWithTimeOffset:offset];
}

- (void)setAnimatedProgress:(CGFloat)animatedProgress {
- (void)setAnimatedProgress:(CGFloat)animatedProgress updateAnimation:(BOOL)updateAnimation {

if (_playFromBeginning) {
_playFromBeginning = NO;
}
_animatedProgress = animatedProgress > 1 ? fmod(animatedProgress, 1) : MAX(animatedProgress, 0);
_animationIsPlaying = NO;
CFTimeInterval offset = _animatedProgress == 1 ? _animationDuration - LOT_singleFrameTimeValue : _animatedProgress * _animationDuration;
__unused CFTimeInterval clock = CACurrentMediaTime();
[self updateAnimationLayerWithTimeOffset:offset];

if (updateAnimation) {
CFTimeInterval offset = _animatedProgress == 1 ? _animationDuration - LOT_singleFrameTimeValue : _animatedProgress * _animationDuration;
__unused CFTimeInterval clock = CACurrentMediaTime();
[self updateAnimationLayerWithTimeOffset:offset];
}
}

- (void)setAnimationSpeed:(CGFloat)speed {
- (void)setAnimationSpeed:(CGFloat)speed updateAnimation:(BOOL)updateAnimation {
_animationSpeed = speed;
CFTimeInterval offset = [_layer convertTime:CACurrentMediaTime() fromLayer:nil];
__unused CFTimeInterval clock = CACurrentMediaTime();
[self updateAnimationLayerWithTimeOffset:offset];

if (updateAnimation) {
CFTimeInterval offset = [_layer convertTime:CACurrentMediaTime() fromLayer:nil];
__unused CFTimeInterval clock = CACurrentMediaTime();
[self updateAnimationLayerWithTimeOffset:offset];
}
}

#pragma mark -- Getters
Expand Down Expand Up @@ -262,17 +279,17 @@ - (instancetype)initWithModel:(LOTComposition *)model {
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR

- (void)_initializeAnimationContainer {
_timingLayer = [CALayer new];
[self.layer addSublayer:_timingLayer];
self.clipsToBounds = YES;
_timingLayer = [CALayer new];
[self.layer addSublayer:_timingLayer];
self.clipsToBounds = YES;
}

#else

- (void)_initializeAnimationContainer {
self.wantsLayer = YES;
_timingLayer = [CALayer new];
[self.layer addSublayer:_timingLayer];
self.wantsLayer = YES;
_timingLayer = [CALayer new];
[self.layer addSublayer:_timingLayer];
}

#endif
Expand Down Expand Up @@ -391,7 +408,7 @@ - (void)setAnimationProgress:(CGFloat)animationProgress {
[self _callCompletionIfNecessary];
}

[_animationState setAnimatedProgress:animationProgress];
[_animationState setAnimatedProgress:animationProgress updateAnimation:self.window != nil];

[self stopDisplayLink];
}
Expand All @@ -401,15 +418,15 @@ - (CGFloat)animationProgress {
}

- (void)setLoopAnimation:(BOOL)loopAnimation {
[_animationState setAnimationDoesLoop:loopAnimation];
[_animationState setAnimationDoesLoop:loopAnimation updateAnimation:self.window != nil];
}

- (BOOL)loopAnimation {
return _animationState.loopAnimation;
}

-(void)setAnimationSpeed:(CGFloat)animationSpeed {
[_animationState setAnimationSpeed:animationSpeed];
[_animationState setAnimationSpeed:animationSpeed updateAnimation:self.window != nil];
}

- (CGFloat)animationSpeed {
Expand Down Expand Up @@ -445,12 +462,16 @@ - (CGFloat)animationDuration {

- (void)didMoveToWindow {
[super didMoveToWindow];
[_animationState setNeedsAnimationUpdate];
if (self.window) {
[_animationState setNeedsAnimationUpdate];
}
}

- (void)didMoveToSuperview {
[super didMoveToSuperview];
[_animationState setNeedsAnimationUpdate];
if (self.window) {
[_animationState setNeedsAnimationUpdate];
}
}

- (void)removeFromSuperview {
Expand All @@ -472,12 +493,12 @@ - (void)layoutSubviews {

- (void)setCompletionBlock:(LOTAnimationCompletionBlock)completionBlock {
if (completionBlock) {
_completionBlock = ^(BOOL finished) {
dispatch_async(dispatch_get_main_queue(), ^{ completionBlock(finished); });
};
_completionBlock = ^(BOOL finished) {
dispatch_async(dispatch_get_main_queue(), ^{ completionBlock(finished); });
};
}
else {
_completionBlock = nil;
_completionBlock = nil;
}
}

Expand All @@ -487,26 +508,26 @@ - (void)setContentMode:(LOTViewContentMode)contentMode {
}

- (void)setNeedsLayout {
self.needsLayout = YES;
self.needsLayout = YES;
}

- (BOOL)isFlipped {
return YES;
return YES;
}

- (BOOL)wantsUpdateLayer {
return YES;
return YES;
}

- (void)layout {
[super layout];
[self _layout];
[super layout];
[self _layout];
}

#endif

- (CGSize)intrinsicContentSize {
return _sceneModel.compBounds.size;
return _sceneModel.compBounds.size;
}

- (void)_layout {
Expand Down

0 comments on commit 554320b

Please sign in to comment.