Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Flatten the animate internal logic. (#55)
Browse files Browse the repository at this point in the history
The internal logic has been flattened to make use of early exiting logic rather than a nested conditional. This will allow us to pull the logic out into smaller methods in order to make the code more self-documenting.
  • Loading branch information
jverkoey authored Nov 22, 2017
1 parent 044ad18 commit 47bee41
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
61 changes: 33 additions & 28 deletions src/MDMMotionAnimator.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,49 +64,54 @@ - (void)animateWithTiming:(MDMMotionTiming)timing
[layer setValue:[values lastObject] forKeyPath:keyPath];
[CATransaction commit];

if (timing.duration == 0 || timing.curve.type == MDMMotionCurveTypeInstant) {
void (^exitEarly)(void) = ^{
if (completion) {
completion();
}
return;
}
};

CGFloat timeScaleFactor = [self computedTimeScaleFactor];
CABasicAnimation *animation = MDMAnimationFromTiming(timing, timeScaleFactor);

if (animation) {
animation.keyPath = keyPath;
if (animation == nil) {
exitEarly();
return;
}

animation.keyPath = keyPath;

id initialValue;
if (_beginFromCurrentState) {
if ([layer presentationLayer]) {
initialValue = [[layer presentationLayer] valueForKeyPath:keyPath];
} else {
initialValue = [layer valueForKeyPath:keyPath];
}
id initialValue;
if (_beginFromCurrentState) {
if ([layer presentationLayer]) {
initialValue = [[layer presentationLayer] valueForKeyPath:keyPath];
} else {
initialValue = [values firstObject];
initialValue = [layer valueForKeyPath:keyPath];
}
} else {
initialValue = [values firstObject];
}

animation.fromValue = initialValue;
animation.toValue = [values lastObject];

animation.fromValue = initialValue;
animation.toValue = [values lastObject];
if ([animation.fromValue isEqual:animation.toValue]) {
exitEarly();
return;
}

if (![animation.fromValue isEqual:animation.toValue]) {
MDMConfigureAnimation(animation, self.additive, timing);
MDMConfigureAnimation(animation, self.additive, timing);

if (timing.delay != 0) {
animation.beginTime = ([layer convertTime:CACurrentMediaTime() fromLayer:nil]
+ timing.delay * timeScaleFactor);
animation.fillMode = kCAFillModeBackwards;
}
if (timing.delay != 0) {
animation.beginTime = ([layer convertTime:CACurrentMediaTime() fromLayer:nil]
+ timing.delay * timeScaleFactor);
animation.fillMode = kCAFillModeBackwards;
}

NSString *key = _additive ? nil : keyPath;
[_registrar addAnimation:animation toLayer:layer forKey:key completion:completion];
NSString *key = _additive ? nil : keyPath;
[_registrar addAnimation:animation toLayer:layer forKey:key completion:completion];

for (void (^tracer)(CALayer *, CAAnimation *) in _tracers) {
tracer(layer, animation);
}
}
for (void (^tracer)(CALayer *, CAAnimation *) in _tracers) {
tracer(layer, animation);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/private/CABasicAnimation+MotionAnimator.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
animation = [CABasicAnimation animation];
animation.timingFunction = MDMTimingFunctionWithControlPoints(timing.curve.data);
animation.duration = timing.duration * timeScaleFactor;

if (animation.duration == 0) {
return nil;
}
break;

case MDMMotionCurveTypeSpring: {
Expand Down

0 comments on commit 47bee41

Please sign in to comment.