From 50d8a99062a568d58a53e3207398c132ee47116c Mon Sep 17 00:00:00 2001 From: William Candillon Date: Fri, 12 Jul 2019 07:23:49 +0200 Subject: [PATCH] =?UTF-8?q?fix(=F0=9F=91=8B=F0=9F=8F=BB):=20Minor=20improv?= =?UTF-8?q?ement=20in=20preserveOffset=20(#78)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Gesture.ts | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Gesture.ts b/src/Gesture.ts index f63f8751..a8d93350 100644 --- a/src/Gesture.ts +++ b/src/Gesture.ts @@ -1,6 +1,6 @@ import Animated from "react-native-reanimated"; import { - State as GestureState, + State, PanGestureHandlerEventExtra, GestureHandlerStateChangeNativeEvent, PinchGestureHandlerEventExtra, @@ -32,16 +32,15 @@ const { export const preserveOffset = ( value: Animated.Adaptable, - state: Animated.Adaptable + state: Animated.Adaptable, + offset: Animated.Value = new Value(0) ) => { const previous = new Value(0); - const offset = new Value(0); - return block([ cond( - eq(state, GestureState.BEGAN), - [set(previous, 0)], - [set(offset, add(offset, sub(value, previous))), set(previous, value)] + eq(state, State.ACTIVE), + [set(offset, add(offset, sub(value, previous))), set(previous, value)], + [set(previous, 0)] ), offset ]); @@ -49,7 +48,7 @@ export const preserveOffset = ( export const decay = ( value: Animated.Adaptable, - state: Animated.Adaptable, + state: Animated.Adaptable, velocity: Animated.Adaptable ) => { const decayedValue = new Value(0); @@ -59,7 +58,7 @@ export const decay = ( return block([ cond( - eq(state, GestureState.END), + eq(state, State.END), [ set( decayedValue, @@ -68,7 +67,7 @@ export const decay = ( ], [ stopClock(clock), - cond(eq(state, GestureState.BEGAN), [ + cond(eq(state, State.BEGAN), [ set(rerunDecaying, 0), set(offset, sub(decayedValue, value)) ]), @@ -81,7 +80,7 @@ export const decay = ( export const spring = ( translation: Animated.Value, - state: Animated.Value, + state: Animated.Value, snapPoint: Animated.Adaptable, defaultOffset: number = 0, springConfig?: Animated.SpringConfig @@ -102,7 +101,7 @@ export const spring = ( }; return block([ cond( - eq(state, GestureState.END), + eq(state, State.END), [ set( springedValue, @@ -111,7 +110,7 @@ export const spring = ( ], [ stopClock(clock), - cond(eq(state, GestureState.BEGAN), [ + cond(eq(state, State.BEGAN), [ set(rerunSpring, 0), set(offset, sub(springedValue, translation)) ]), @@ -124,7 +123,7 @@ export const spring = ( export const limit = ( value: Animated.Adaptable, - state: Animated.Adaptable, + state: Animated.Adaptable, min: Animated.Adaptable, max: Animated.Adaptable ) => { @@ -132,7 +131,7 @@ export const limit = ( const offsetValue = add(offset, value); return block([ - cond(eq(state, GestureState.BEGAN), [ + cond(eq(state, State.BEGAN), [ cond(lessThan(offsetValue, min), set(offset, sub(min, value))), cond(greaterThan(offsetValue, max), set(offset, sub(max, value))) ]), @@ -153,7 +152,7 @@ export const preserveMultiplicativeOffset = ( return block([ cond( - eq(state, GestureState.BEGAN), + eq(state, State.BEGAN), [set(previous, 1)], [ set(offset, multiply(offset, divide(value, previous))),