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

Commit b2a0f96

Browse files
authoredJul 21, 2017
Add a timeScaleFactor API to the animator. (#6)
This API allows you to scale the durations and delay of all animations. It can also be used to build animations that make use of relative time, e.g. durations/delays in terms of 0-1 and timeScaleFactor the actual duration.
1 parent 9bdf4f6 commit b2a0f96

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed
 

‎src/MDMMotionAnimator.h

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727
NS_SWIFT_NAME(MotionAnimator)
2828
@interface MDMMotionAnimator : NSObject
2929

30+
/**
31+
The scaling factor to apply to all time-related values.
32+
33+
For example, a timeScaleFactor of 2 will double the length of all animations.
34+
35+
1.0 by default.
36+
*/
37+
@property(nonatomic, assign) CGFloat timeScaleFactor;
38+
3039
/**
3140
If enabled, all animations will be added with their values reversed.
3241

‎src/MDMMotionAnimator.m

+7-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static CGFloat simulatorAnimationDragCoefficient(void) {
3232

3333
static CAMediaTimingFunction* timingFunctionWithControlPoints(CGFloat controlPoints[4]);
3434
static NSArray* coerceUIKitValuesToCoreAnimationValues(NSArray *values);
35-
static CABasicAnimation *animationFromTiming(MDMMotionTiming timing);
35+
static CABasicAnimation *animationFromTiming(MDMMotionTiming timing, CGFloat timeScaleFactor);
3636
static void makeAnimationAdditive(CABasicAnimation *animation);
3737

3838
@implementation MDMMotionAnimator {
@@ -42,6 +42,7 @@ @implementation MDMMotionAnimator {
4242
- (instancetype)init {
4343
self = [super init];
4444
if (self) {
45+
_timeScaleFactor = 1;
4546
_additive = true;
4647
}
4748
return self;
@@ -74,7 +75,8 @@ - (void)animateWithTiming:(MDMMotionTiming)timing
7475
return;
7576
}
7677

77-
CABasicAnimation *animation = animationFromTiming(timing);
78+
CGFloat timeScaleFactor = simulatorAnimationDragCoefficient() * _timeScaleFactor;
79+
CABasicAnimation *animation = animationFromTiming(timing, timeScaleFactor);
7880

7981
if (animation) {
8082
animation.keyPath = keyPath;
@@ -100,7 +102,7 @@ - (void)animateWithTiming:(MDMMotionTiming)timing
100102

101103
if (timing.delay != 0) {
102104
animation.beginTime = ([layer convertTime:CACurrentMediaTime() fromLayer:nil]
103-
+ timing.delay * simulatorAnimationDragCoefficient());
105+
+ timing.delay * timeScaleFactor);
104106
animation.fillMode = kCAFillModeBackwards;
105107
}
106108

@@ -160,7 +162,7 @@ - (void)addCoreAnimationTracer:(void (^)(CALayer *, CAAnimation *))tracer {
160162
return values;
161163
}
162164

163-
static CABasicAnimation *animationFromTiming(MDMMotionTiming timing) {
165+
static CABasicAnimation *animationFromTiming(MDMMotionTiming timing, CGFloat timeScaleFactor) {
164166
CABasicAnimation *animation;
165167
switch (timing.curve.type) {
166168
case MDMMotionCurveTypeInstant:
@@ -171,7 +173,7 @@ - (void)addCoreAnimationTracer:(void (^)(CALayer *, CAAnimation *))tracer {
171173
case MDMMotionCurveTypeBezier:
172174
animation = [CABasicAnimation animation];
173175
animation.timingFunction = timingFunctionWithControlPoints(timing.curve.data);
174-
animation.duration = timing.duration * simulatorAnimationDragCoefficient();
176+
animation.duration = timing.duration * timeScaleFactor;
175177
break;
176178

177179
case MDMMotionCurveTypeSpring: {

0 commit comments

Comments
 (0)
This repository has been archived.