Skip to content

Commit 66e2659

Browse files
authored
feat(👋🏻): add onGestureEvent() (#62)
1 parent d1b9730 commit 66e2659

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ return (
104104
### `getLengthAtX(path: ReanimatedPath, x: Node): Node`
105105

106106
Convenience function for bezier curves where there is really only ever one "y" value associated with one "x" value.
107-
This function works by finding the roots of the cubic bezier curve so it is a somewhat compute-intensive.
107+
This function works by finding the roots of the cubic bezier curve so it might be too compute-intensive to calculate for each frame.
108108

109109
## Math
110110

@@ -350,6 +350,21 @@ And for an horizontal one.
350350
<Animated.ScrollView onScroll={onScroll({ x: new Value(0) })} horizontal />
351351
```
352352

353+
### `gestureEvent(nativeEvent)`
354+
355+
Returns a reanimated event handler for any Gesture handler event handler.
356+
357+
Example usage for a vertical `PanGestureHandler`.
358+
359+
```js
360+
const translationX = new Value(0);
361+
const state = new Value(State.UNDETERMITED);
362+
const onGestureEvent = gestureEvent({ translationX, state })
363+
return (
364+
<PanGestureHandler onHandlerStateChange={onGestureEvent} {...{onGestureEvent}} />
365+
);
366+
```
367+
353368
### decay
354369

355370
Decorates animated value to decay after pan

src/Animations.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ const {
1717
export const snapPoint = (
1818
value: Animated.Adaptable<number>,
1919
velocity: Animated.Adaptable<number>,
20-
points: number[]
20+
points: Animated.Adaptable<number>[]
2121
) => {
2222
const point = add(value, multiply(0.2, velocity));
2323
const diffPoint = (p: Animated.Adaptable<number>) => abs(sub(point, p));
2424
const deltas = points.map(p => diffPoint(p));
2525
const minDelta = min(...deltas);
2626
return points.reduce(
27-
(acc: Animated.Node<any>, p: number) =>
28-
cond(eq(diffPoint(p), minDelta), p, acc),
27+
(acc, p) => cond(eq(diffPoint(p), minDelta), p, acc),
2928
new Value()
3029
);
3130
};

src/Gesture.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import Animated from "react-native-reanimated";
2-
import { State as GestureState } from "react-native-gesture-handler";
2+
import {
3+
State as GestureState,
4+
PanGestureHandlerEventExtra,
5+
GestureHandlerStateChangeNativeEvent,
6+
PinchGestureHandlerEventExtra,
7+
RotationGestureHandlerEventExtra,
8+
TapGestureHandlerEventExtra,
9+
ForceTouchGestureHandlerEventExtra,
10+
LongPressGestureHandlerEventExtra,
11+
FlingGestureHandlerEventExtra
12+
} from "react-native-gesture-handler";
313

414
import { runDecay, runSpring } from "./AnimationRunners";
515

@@ -164,3 +174,16 @@ export const onScroll = (contentOffset: {
164174
}
165175
}
166176
]);
177+
178+
type NativeEvent = GestureHandlerStateChangeNativeEvent &
179+
(
180+
| PanGestureHandlerEventExtra
181+
| TapGestureHandlerEventExtra
182+
| LongPressGestureHandlerEventExtra
183+
| RotationGestureHandlerEventExtra
184+
| FlingGestureHandlerEventExtra
185+
| PinchGestureHandlerEventExtra
186+
| ForceTouchGestureHandlerEventExtra);
187+
188+
export const gestureEvent = (nativeEvent: Partial<NativeEvent>) =>
189+
event([{ nativeEvent }]);

yarn.lock

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7164,6 +7164,10 @@ split@^1.0.0:
71647164
dependencies:
71657165
through "2"
71667166

7167+
sprintf-js@^1.0.3:
7168+
version "1.1.2"
7169+
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.1.2.tgz#da1765262bf8c0f571749f2ad6c26300207ae673"
7170+
71677171
sprintf-js@~1.0.2:
71687172
version "1.0.3"
71697173
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
@@ -7596,6 +7600,13 @@ umask@^1.1.0, umask@~1.1.0:
75967600
version "1.1.0"
75977601
resolved "https://registry.yarnpkg.com/umask/-/umask-1.1.0.tgz#f29cebf01df517912bb58ff9c4e50fde8e33320d"
75987602

7603+
7604+
version "3.3.5"
7605+
resolved "https://registry.yarnpkg.com/underscore.string/-/underscore.string-3.3.5.tgz#fc2ad255b8bd309e239cbc5816fd23a9b7ea4023"
7606+
dependencies:
7607+
sprintf-js "^1.0.3"
7608+
util-deprecate "^1.0.2"
7609+
75997610
underscore.string@~2.2.0rc:
76007611
version "2.2.1"
76017612
resolved "https://registry.yarnpkg.com/underscore.string/-/underscore.string-2.2.1.tgz#d7c0fa2af5d5a1a67f4253daee98132e733f0f19"
@@ -7720,7 +7731,7 @@ user-home@^2.0.0:
77207731
dependencies:
77217732
os-homedir "^1.0.0"
77227733

7723-
util-deprecate@^1.0.1, util-deprecate@~1.0.1:
7734+
util-deprecate@^1.0.1, util-deprecate@^1.0.2, util-deprecate@~1.0.1:
77247735
version "1.0.2"
77257736
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
77267737

0 commit comments

Comments
 (0)