Skip to content

Commit

Permalink
Add ability to use pinch gesture handle event type (#1194)
Browse files Browse the repository at this point in the history
## Description

<!--
Description and motivation for this PR.

Inlude Fixes #<number> if this is fixing some issue.

Fixes # .
-->

Right now there is no way to use pinch gesture handler with `useAnimatedGestureHandler` in typescript because of wrong typing. This PR fixes it.

Fixes #1164

Now when we want to use PanGestureHandler, we can just use `useAnimatedGestureHandler`, but if you want to use typed context, you need to pass both context type and gesture handler type:

```ts
const handler = useAnimatedGestureHandler<
  PanGestureHandlerGestureEvent,
  {
    someContextProp: number;
  },
>(handlers);
```

For all the other handlers, you need to pass them:

```ts
const tapHandler = useAnimatedGestureHandler<
  TapGestureHandlerGestureEvent,
>(handlers);
```

By default, the context has a type of an empty object.

## Changes

<!--
Please describe things you've changed here, make a **high level** overview, if change is simple you can omit this section.

For example:

- Added `foo` method which add bouncing animation
- Updated `about.md` docs
- Added caching in CI builds

-->

- Typings: Add ability to pass event type to useAnimatedGestureHandler

<!--

## Screenshots / GIFs

Here you can add screenshots / GIFs documenting your change.

You can add before / after section if you're changing some behavior.

### Before

### After

-->
  • Loading branch information
terrysahaidak authored Sep 22, 2020
1 parent 900b0b3 commit f1cca22
Show file tree
Hide file tree
Showing 2 changed files with 264 additions and 180 deletions.
42 changes: 37 additions & 5 deletions react-native-reanimated-tests.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* eslint-disable */
import React from 'react';
import { StyleSheet, Button, View } from 'react-native';
import { PanGestureHandler } from 'react-native-gesture-handler';
import {
PanGestureHandler,
PinchGestureHandlerGestureEvent,
PinchGestureHandler,
PanGestureHandlerGestureEvent,
TapGestureHandlerGestureEvent,
} from 'react-native-gesture-handler';
import Animated, {
useSharedValue,
useDerivedValue,
Expand Down Expand Up @@ -90,14 +96,14 @@ function AnimatedScrollHandlerTest() {
);
}

// useAnimatedGestureHandler
// useAnimatedGestureHandler with context
function AnimatedGestureHandlerTest() {
const x = useSharedValue(0);
const gestureHandler = useAnimatedGestureHandler({
onStart: (_, ctx: { startX: number }) => {
const gestureHandler = useAnimatedGestureHandler<PanGestureHandlerGestureEvent, { startX: number }>({
onStart: (_, ctx) => {
ctx.startX = x.value;
},
onActive: (event, ctx: { startX: number }) => {
onActive: (event, ctx) => {
x.value = ctx.startX + event.translationX;
},
onEnd: (_) => {
Expand All @@ -120,6 +126,32 @@ function AnimatedGestureHandlerTest() {
);
}

function AnimatedPinchGestureHandlerTest() {
const x = useSharedValue(0);
const gestureHandler = useAnimatedGestureHandler<PinchGestureHandlerGestureEvent>({
onActive: (event) => {
x.value = event.scale;
},
onEnd: () => {
x.value = withTiming(1);
},
});
const animatedStyle = useAnimatedStyle(() => {
return {
transform: [
{
scale: x.value,
},
],
};
});
return (
<PinchGestureHandler onGestureEvent={gestureHandler}>
<Animated.View style={[styles.box, animatedStyle]} />
</PinchGestureHandler>
);
}

/**
* Reanimated 2 Animations
*/
Expand Down
Loading

0 comments on commit f1cca22

Please sign in to comment.