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

Flatten the animate internal logic. #55

Merged
merged 2 commits into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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