Skip to content

Commit b26295e

Browse files
committed
feat: add pan gesture to shared value example
1 parent 6647f3e commit b26295e

14 files changed

Lines changed: 53 additions & 64 deletions

example/src/pages/SharedValueListenerExample.tsx

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import {
2-
View,
3-
Text,
4-
StyleSheet,
5-
Button,
6-
ActivityIndicator,
7-
} from 'react-native';
1+
import { View, Text, StyleSheet, ActivityIndicator } from 'react-native';
82
import { type Metadata } from '../helpers/metadata';
93
import Animated, {
104
useSharedValue,
115
useAnimatedReaction,
126
useAnimatedStyle,
137
withSpring,
8+
withTiming,
149
} from 'react-native-reanimated';
15-
import { useEffect, useMemo } from 'react';
10+
import {
11+
Gesture,
12+
GestureDetector,
13+
GestureHandlerRootView,
14+
} from 'react-native-gesture-handler';
15+
import { useMemo } from 'react';
1616
import { NitroModules } from 'react-native-nitro-modules';
1717
import {
1818
Fit,
@@ -68,7 +68,8 @@ function AnimatedRiveExample({
6868
instance: ViewModelInstance;
6969
file: RiveFile;
7070
}) {
71-
const progress = useSharedValue(0);
71+
const pressed = useSharedValue(false);
72+
const offset = useSharedValue(0);
7273

7374
const boxedProperty = useMemo(() => {
7475
const posYProperty = instance.numberProperty('posY');
@@ -78,8 +79,20 @@ function AnimatedRiveExample({
7879
return NitroModules.box(posYProperty);
7980
}, [instance]);
8081

82+
const pan = Gesture.Pan()
83+
.onBegin(() => {
84+
pressed.value = true;
85+
})
86+
.onChange((event) => {
87+
offset.value = event.translationY * 3;
88+
})
89+
.onFinalize(() => {
90+
offset.value = withSpring(0);
91+
pressed.value = false;
92+
});
93+
8194
useAnimatedReaction(
82-
() => progress.value,
95+
() => offset.value,
8396
(value: number) => {
8497
'worklet';
8598
if (!boxedProperty) return;
@@ -90,34 +103,15 @@ function AnimatedRiveExample({
90103
);
91104

92105
const circleStyle = useAnimatedStyle(() => ({
93-
transform: [{ translateY: progress.value / 3 }],
106+
transform: [{ translateY: offset.value / 3 }],
107+
backgroundColor: pressed.value ? '#FFE04B' : 'blue',
108+
scale: withTiming(pressed.value ? 1.2 : 1),
94109
}));
95110

96-
const animateTo800 = () => {
97-
progress.value = 0;
98-
progress.value = withSpring(800, {
99-
damping: 8,
100-
stiffness: 80,
101-
});
102-
};
103-
104-
const animateTo0 = () => {
105-
progress.value = withSpring(0, {
106-
damping: 8,
107-
stiffness: 80,
108-
});
109-
};
110-
111-
useEffect(() => {
112-
animateTo800();
113-
// eslint-disable-next-line react-hooks/exhaustive-deps
114-
}, []);
115-
116111
return (
117-
<View style={styles.container}>
112+
<GestureHandlerRootView style={styles.container}>
118113
<Text style={styles.subtitle}>
119-
Circle posY is driven by Reanimated shared value (Red circle is rive,
120-
Blue circle is React Native View)
114+
Drag the blue circle to move both circles. Release to spring back.
121115
</Text>
122116

123117
<View style={styles.riveContainer}>
@@ -129,14 +123,11 @@ function AnimatedRiveExample({
129123
layoutScaleFactor={1}
130124
file={file}
131125
/>
132-
<Animated.View style={[styles.blueCircle, circleStyle]} />
133-
</View>
134-
135-
<View style={styles.buttonContainer}>
136-
<Button title="Bounce to 800" onPress={animateTo800} />
137-
<Button title="Bounce to 0" onPress={animateTo0} />
126+
<GestureDetector gesture={pan}>
127+
<Animated.View style={[styles.blueCircle, circleStyle]} />
128+
</GestureDetector>
138129
</View>
139-
</View>
130+
</GestureHandlerRootView>
140131
);
141132
}
142133

@@ -150,18 +141,11 @@ const styles = StyleSheet.create({
150141
flex: 1,
151142
backgroundColor: '#fff',
152143
},
153-
title: {
154-
fontSize: 24,
155-
fontWeight: 'bold',
156-
textAlign: 'center',
157-
marginTop: 20,
158-
marginBottom: 10,
159-
},
160144
subtitle: {
161145
fontSize: 16,
162146
color: '#666',
163147
textAlign: 'center',
164-
marginBottom: 20,
148+
marginVertical: 20,
165149
paddingHorizontal: 20,
166150
},
167151
riveContainer: {
@@ -178,12 +162,6 @@ const styles = StyleSheet.create({
178162
textAlign: 'center',
179163
padding: 20,
180164
},
181-
buttonContainer: {
182-
flexDirection: 'row',
183-
justifyContent: 'center',
184-
gap: 20,
185-
padding: 20,
186-
},
187165
blueCircle: {
188166
position: 'absolute',
189167
left: 50,

nitrogen/generated/android/c++/JFunc_void.hpp

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/android/c++/JFunc_void_RiveError.hpp

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/android/c++/JFunc_void_UnifiedRiveEvent.hpp

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/android/c++/JFunc_void_bool.hpp

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/android/c++/JFunc_void_double.hpp

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/android/c++/JFunc_void_std__string.hpp

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/android/c++/JHybridRiveViewSpec.cpp

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/android/c++/JHybridViewModelBooleanPropertySpec.cpp

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/android/c++/JHybridViewModelColorPropertySpec.cpp

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)