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

Commit 47bee41

Browse files
authored
Flatten the animate internal logic. (#55)
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.
1 parent 044ad18 commit 47bee41

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

src/MDMMotionAnimator.m

+33-28
Original file line numberDiff line numberDiff line change
@@ -64,49 +64,54 @@ - (void)animateWithTiming:(MDMMotionTiming)timing
6464
[layer setValue:[values lastObject] forKeyPath:keyPath];
6565
[CATransaction commit];
6666

67-
if (timing.duration == 0 || timing.curve.type == MDMMotionCurveTypeInstant) {
67+
void (^exitEarly)(void) = ^{
6868
if (completion) {
6969
completion();
7070
}
71-
return;
72-
}
71+
};
7372

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

77-
if (animation) {
78-
animation.keyPath = keyPath;
76+
if (animation == nil) {
77+
exitEarly();
78+
return;
79+
}
80+
81+
animation.keyPath = keyPath;
7982

80-
id initialValue;
81-
if (_beginFromCurrentState) {
82-
if ([layer presentationLayer]) {
83-
initialValue = [[layer presentationLayer] valueForKeyPath:keyPath];
84-
} else {
85-
initialValue = [layer valueForKeyPath:keyPath];
86-
}
83+
id initialValue;
84+
if (_beginFromCurrentState) {
85+
if ([layer presentationLayer]) {
86+
initialValue = [[layer presentationLayer] valueForKeyPath:keyPath];
8787
} else {
88-
initialValue = [values firstObject];
88+
initialValue = [layer valueForKeyPath:keyPath];
8989
}
90+
} else {
91+
initialValue = [values firstObject];
92+
}
93+
94+
animation.fromValue = initialValue;
95+
animation.toValue = [values lastObject];
9096

91-
animation.fromValue = initialValue;
92-
animation.toValue = [values lastObject];
97+
if ([animation.fromValue isEqual:animation.toValue]) {
98+
exitEarly();
99+
return;
100+
}
93101

94-
if (![animation.fromValue isEqual:animation.toValue]) {
95-
MDMConfigureAnimation(animation, self.additive, timing);
102+
MDMConfigureAnimation(animation, self.additive, timing);
96103

97-
if (timing.delay != 0) {
98-
animation.beginTime = ([layer convertTime:CACurrentMediaTime() fromLayer:nil]
99-
+ timing.delay * timeScaleFactor);
100-
animation.fillMode = kCAFillModeBackwards;
101-
}
104+
if (timing.delay != 0) {
105+
animation.beginTime = ([layer convertTime:CACurrentMediaTime() fromLayer:nil]
106+
+ timing.delay * timeScaleFactor);
107+
animation.fillMode = kCAFillModeBackwards;
108+
}
102109

103-
NSString *key = _additive ? nil : keyPath;
104-
[_registrar addAnimation:animation toLayer:layer forKey:key completion:completion];
110+
NSString *key = _additive ? nil : keyPath;
111+
[_registrar addAnimation:animation toLayer:layer forKey:key completion:completion];
105112

106-
for (void (^tracer)(CALayer *, CAAnimation *) in _tracers) {
107-
tracer(layer, animation);
108-
}
109-
}
113+
for (void (^tracer)(CALayer *, CAAnimation *) in _tracers) {
114+
tracer(layer, animation);
110115
}
111116
}
112117

src/private/CABasicAnimation+MotionAnimator.m

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
animation = [CABasicAnimation animation];
3636
animation.timingFunction = MDMTimingFunctionWithControlPoints(timing.curve.data);
3737
animation.duration = timing.duration * timeScaleFactor;
38+
39+
if (animation.duration == 0) {
40+
return nil;
41+
}
3842
break;
3943

4044
case MDMMotionCurveTypeSpring: {

0 commit comments

Comments
 (0)