Skip to content

Commit

Permalink
Add delay support to Animated.spring
Browse files Browse the repository at this point in the history
Summary:
Aadding a `delay` option to `Animated.spring` works now 👇:

![spring_delay](https://user-images.githubusercontent.com/18269100/28255417-7650233e-6a6b-11e7-87a3-ed15794b9ed8.gif)
Closes #15043

Differential Revision: D5436307

Pulled By: hramos

fbshipit-source-id: df0652d20ee5810986b322486f1ec417fe2d0a0a
  • Loading branch information
billoosijok authored and facebook-github-bot committed Jul 18, 2017
1 parent b60a8dc commit 9c2ce53
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions Libraries/Animated/src/AnimatedImplementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ type SpringAnimationConfig = AnimationConfig & {
speed?: number,
tension?: number,
friction?: number,
delay?: number,
};

type SpringAnimationConfigSingle = AnimationConfig & {
Expand All @@ -471,6 +472,7 @@ type SpringAnimationConfigSingle = AnimationConfig & {
speed?: number,
tension?: number,
friction?: number,
delay?: number,
};

function withDefault<T>(value: ?T, defaultValue: T): T {
Expand All @@ -492,6 +494,8 @@ class SpringAnimation extends Animation {
_toValue: any;
_tension: number;
_friction: number;
_delay: number;
_timeout: any;
_lastTime: number;
_onUpdate: (value: number) => void;
_animationFrame: any;
Expand All @@ -508,6 +512,7 @@ class SpringAnimation extends Animation {
this._initialVelocity = config.velocity;
this._lastVelocity = withDefault(config.velocity, 0);
this._toValue = config.toValue;
this._delay = withDefault(config.delay, 0);
this._useNativeDriver = shouldUseNativeDriver(config);
this.__isInteraction = config.isInteraction !== undefined ? config.isInteraction : true;
this.__iterations = config.iterations !== undefined ? config.iterations : 1;
Expand Down Expand Up @@ -571,10 +576,20 @@ class SpringAnimation extends Animation {
this._initialVelocity !== null) {
this._lastVelocity = this._initialVelocity;
}
if (this._useNativeDriver) {
this.__startNativeAnimation(animatedValue);

var start = () => {
if (this._useNativeDriver) {
this.__startNativeAnimation(animatedValue);
} else {
this.onUpdate();
}
};

// If this._delay is more than 0, we start after the timeout.
if (this._delay) {
this._timeout = setTimeout(start, this._delay);
} else {
this.onUpdate();
start();
}
}

Expand Down Expand Up @@ -685,6 +700,7 @@ class SpringAnimation extends Animation {
stop(): void {
super.stop();
this.__active = false;
clearTimeout(this._timeout);
global.cancelAnimationFrame(this._animationFrame);
this.__debouncedOnEnd({finished: false});
}
Expand Down

0 comments on commit 9c2ce53

Please sign in to comment.