[Animated] Send a final update with toValue for spring
Summary: Animated.spring is not guarantee to stabilize at exactly toValue (determined by restDisplacementThreshold). It is a bit annoying that the last value is not toValue, it makes the logs harder to read and also prevents you from writing code like value === toValue. Instead you need to track it down somewhere else.
This commit is contained in:
parent
b34892eb80
commit
fea2db42fd
|
@ -479,7 +479,13 @@ class SpringAnimation extends Animation {
|
||||||
if (this._tension !== 0) {
|
if (this._tension !== 0) {
|
||||||
isDisplacement = Math.abs(this._toValue - position) <= this._restDisplacementThreshold;
|
isDisplacement = Math.abs(this._toValue - position) <= this._restDisplacementThreshold;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isOvershooting || (isVelocity && isDisplacement)) {
|
if (isOvershooting || (isVelocity && isDisplacement)) {
|
||||||
|
if (this._tension !== 0) {
|
||||||
|
// Ensure that we end up with a round value
|
||||||
|
this._onUpdate(this._toValue);
|
||||||
|
}
|
||||||
|
|
||||||
this.__debouncedOnEnd({finished: true});
|
this.__debouncedOnEnd({finished: true});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,6 +127,19 @@ describe('Animated', () => {
|
||||||
Animated.spring(anim, {toValue: 0, velocity: 0}).start(callback);
|
Animated.spring(anim, {toValue: 0, velocity: 0}).start(callback);
|
||||||
expect(callback).toBeCalled();
|
expect(callback).toBeCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('send toValue when a spring stops', () => {
|
||||||
|
var anim = new Animated.Value(0);
|
||||||
|
var listener = jest.genMockFunction();
|
||||||
|
anim.addListener(listener);
|
||||||
|
Animated.spring(anim, {toValue: 15}).start();
|
||||||
|
jest.runAllTimers();
|
||||||
|
var lastValue = listener.mock.calls[listener.mock.calls.length - 2][0].value;
|
||||||
|
expect(lastValue).not.toBe(15);
|
||||||
|
expect(lastValue).toBeCloseTo(15);
|
||||||
|
expect(anim.__getValue()).toBe(15);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue