Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes setting initial progress of Lottie animation #245

Merged
merged 3 commits into from
Jun 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 _callCompletionIfNecesarry];
}

[_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