|
| 1 | +--- |
| 2 | +id: animated-interactions |
| 3 | +title: Integration with Animated |
| 4 | +sidebar_label: Integration with Animated |
| 5 | +sidebar_position: 5 |
| 6 | +--- |
| 7 | + |
| 8 | +import CollapsibleCode from '@site/src/components/CollapsibleCode'; |
| 9 | + |
| 10 | +Using hook API allows for smooth integration with the [Animated API](https://reactnative.dev/docs/animated) by allowing for passing an `Animated.event` as the argument to the `onUpdate` callback. The event mapping of `Animated.event` depends on the `useNativeDriver` property. |
| 11 | + |
| 12 | +:::danger Mixing Reanimated and Animated |
| 13 | +It is not possible to mix `Reanimated` and `Animated` within any of the [gesture detectors](/docs/fundamentals/gesture-detectors). |
| 14 | +::: |
| 15 | + |
| 16 | +<CollapsibleCode |
| 17 | +label="Show full example" |
| 18 | +expandedLabel="Hide full example" |
| 19 | +lineBounds={[8, 31]} |
| 20 | +src={` |
| 21 | +import * as React from 'react'; |
| 22 | +import { Animated, StyleSheet, useAnimatedValue } from 'react-native'; |
| 23 | +import { |
| 24 | + GestureHandlerRootView, |
| 25 | + GestureDetector, |
| 26 | + usePanGesture, |
| 27 | +} from 'react-native-gesture-handler'; |
| 28 | +
|
| 29 | +export default function App() { |
| 30 | + const value = useAnimatedValue(0); |
| 31 | +
|
| 32 | + const event = Animated.event( |
| 33 | + [{ nativeEvent: { handlerData: { translationX: value } } }], |
| 34 | + { |
| 35 | + useNativeDriver: true, |
| 36 | + } |
| 37 | + ); |
| 38 | +
|
| 39 | + const gesture = usePanGesture({ |
| 40 | + // highlight-next-line |
| 41 | + onUpdate: event, |
| 42 | + }); |
| 43 | +
|
| 44 | + return ( |
| 45 | + <GestureHandlerRootView> |
| 46 | + <GestureDetector gesture={gesture}> |
| 47 | + <Animated.View |
| 48 | + style={[styles.box, { transform: [{ translateX: value }] }]} |
| 49 | + /> |
| 50 | + </GestureDetector> |
| 51 | + </GestureHandlerRootView> |
| 52 | + ); |
| 53 | +} |
| 54 | +
|
| 55 | +const styles = StyleSheet.create({ |
| 56 | + box: { |
| 57 | + width: 150, |
| 58 | + height: 150, |
| 59 | + backgroundColor: '#b58df1', |
| 60 | + }, |
| 61 | +}); |
| 62 | +`}/> |
| 63 | + |
| 64 | +## useNativeDriver |
| 65 | + |
| 66 | +When using `Animated.event` with `useNativeDriver` set to `false`, it is required to set [`disableReanimated`](/docs/fundamentals/reanimated-interactions#disablereanimated) to `true` in the gesture configuration. |
| 67 | + |
| 68 | +Mapping of `Animated.event` depends on the value of `useNativeDriver` property. When set to `true`, event data can be accessed through `nativeEvent.handlerData` property: |
| 69 | + |
| 70 | +```jsx |
| 71 | + const value = useAnimatedValue(0); |
| 72 | + |
| 73 | + const event = Animated.event( |
| 74 | + [{ nativeEvent: { handlerData: { /* translationX: value, ... */ } } }], |
| 75 | + { useNativeDriver: true } |
| 76 | + ); |
| 77 | +``` |
| 78 | + |
| 79 | +In case of `useNativeDriver` set to `false`, event data is accessed directly: |
| 80 | + |
| 81 | +```jsx |
| 82 | + const value = useAnimatedValue(0); |
| 83 | + |
| 84 | + const event = Animated.event( |
| 85 | + [ { /* translationX: value, ... */ } ], |
| 86 | + { useNativeDriver: false } |
| 87 | + ); |
| 88 | +``` |
| 89 | + |
| 90 | +## Usage with VirtualGestureDetector |
| 91 | + |
| 92 | +Using `Animated.event` with [`VirtualGestureDetector`](/docs/fundamentals/gesture-detectors#virtualgesturedetector) is possible only when `useNativeDriver` is set to `false`. |
| 93 | + |
| 94 | +<CollapsibleCode |
| 95 | +label="Show full example" |
| 96 | +expandedLabel="Hide full example" |
| 97 | +lineBounds={[8, 33]} |
| 98 | +src={` |
| 99 | +import React from 'react'; |
| 100 | +import { View, StyleSheet, Animated, useAnimatedValue } from 'react-native'; |
| 101 | +import { |
| 102 | + GestureHandlerRootView, |
| 103 | + InterceptingGestureDetector, |
| 104 | + usePanGesture, |
| 105 | + VirtualGestureDetector, |
| 106 | +} from 'react-native-gesture-handler'; |
| 107 | +
|
| 108 | +export default function App() { |
| 109 | + const value = useAnimatedValue(0); |
| 110 | + const event = Animated.event([{ translationX: value }], { |
| 111 | + useNativeDriver: false, |
| 112 | + }); |
| 113 | +
|
| 114 | + const panGesture = usePanGesture({ |
| 115 | + onUpdate: event, |
| 116 | + disableReanimated: true, |
| 117 | + }); |
| 118 | +
|
| 119 | + return ( |
| 120 | + <GestureHandlerRootView style={styles.container}> |
| 121 | + <InterceptingGestureDetector> |
| 122 | + <View style={styles.outerBox}> |
| 123 | + <VirtualGestureDetector gesture={panGesture}> |
| 124 | + <Animated.View |
| 125 | + style={[styles.innerBox, { transform: [{ translateX: value }] }]} |
| 126 | + /> |
| 127 | + </VirtualGestureDetector> |
| 128 | + </View> |
| 129 | + </InterceptingGestureDetector> |
| 130 | + </GestureHandlerRootView> |
| 131 | + ); |
| 132 | +} |
| 133 | +
|
| 134 | +const styles = StyleSheet.create({ |
| 135 | + container: { |
| 136 | + flex: 1, |
| 137 | + alignItems: 'center', |
| 138 | + justifyContent: 'center', |
| 139 | + }, |
| 140 | + outerBox: { |
| 141 | + backgroundColor: '#b58df1', |
| 142 | + width: 150, |
| 143 | + height: 150, |
| 144 | + }, |
| 145 | + innerBox: { |
| 146 | + width: 100, |
| 147 | + height: 100, |
| 148 | + backgroundColor: 'blue', |
| 149 | + }, |
| 150 | +}); |
| 151 | +`}/> |
0 commit comments