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

Commit 45f355f

Browse files
authored
Check for divide-by-zero before calculating the initial velocity of spring animations. (#75)
This improves the robustness of MDMConfigureAnimation while enabling us to call the method without having to check whether the from/to value are equal.
1 parent 2692850 commit 45f355f

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/private/CABasicAnimation+MotionAnimator.h

-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,4 @@ FOUNDATION_EXPORT BOOL MDMCanAnimationBeAdditive(NSString *keyPath, id toValue);
3333
//
3434
// Not all animation value types support being additive. If an animation's value type was not
3535
// supported, the animation's values will not be modified.
36-
//
37-
// If the from and to values of the animation match then the behavior is undefined.
3836
FOUNDATION_EXPORT void MDMConfigureAnimation(CABasicAnimation *animation, MDMMotionTiming timing);

src/private/CABasicAnimation+MotionAnimator.m

+9-3
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ void MDMConfigureAnimation(CABasicAnimation *animation, MDMMotionTiming timing)
180180
// As for our sign, if absoluteInitialVelocity matches the direction of displacement, then our
181181
// sign will be positive. Otherwise, our sign will be negative, as expected by Core Animation.
182182

183-
springAnimation.initialVelocity = absoluteInitialVelocity / displacement;
183+
if (fabs(displacement) > 0.00001) {
184+
springAnimation.initialVelocity = absoluteInitialVelocity / displacement;
185+
}
184186
}
185187

186188
} else if (IsCGSizeType(animation.toValue)) {
@@ -211,7 +213,9 @@ void MDMConfigureAnimation(CABasicAnimation *animation, MDMMotionTiming timing)
211213
CGFloat displacement = -biggestDelta;
212214
CGFloat absoluteInitialVelocity =
213215
timing.curve.data[MDMSpringMotionCurveDataIndexInitialVelocity];
214-
springAnimation.initialVelocity = absoluteInitialVelocity / displacement;
216+
if (fabs(displacement) > 0.00001) {
217+
springAnimation.initialVelocity = absoluteInitialVelocity / displacement;
218+
}
215219
}
216220

217221
} else if (IsCGPointType(animation.toValue)) {
@@ -242,7 +246,9 @@ void MDMConfigureAnimation(CABasicAnimation *animation, MDMMotionTiming timing)
242246
CGFloat displacement = -biggestDelta;
243247
CGFloat absoluteInitialVelocity =
244248
timing.curve.data[MDMSpringMotionCurveDataIndexInitialVelocity];
245-
springAnimation.initialVelocity = absoluteInitialVelocity / displacement;
249+
if (fabs(displacement) > 0.00001) {
250+
springAnimation.initialVelocity = absoluteInitialVelocity / displacement;
251+
}
246252
}
247253
}
248254

0 commit comments

Comments
 (0)