-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathScrollEventExample.tsx
92 lines (84 loc) · 2.16 KB
/
ScrollEventExample.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import React from 'react';
import { View, StyleSheet, Platform, Dimensions } from 'react-native';
import Animated, {
useSharedValue,
withSpring,
useAnimatedStyle,
useAnimatedScrollHandler,
} from 'react-native-reanimated';
import { useHeaderHeight } from '@react-navigation/elements';
const size = 40;
function ScrollExample(): React.ReactElement {
const transY = useSharedValue(0);
const isScrolling = useSharedValue(false);
const headerHeight = useHeaderHeight();
const scrollHandler = useAnimatedScrollHandler({
onScroll: (event) => {
transY.value = event.contentOffset.y;
},
onBeginDrag: () => {
isScrolling.value = true;
},
onEndDrag: () => {
isScrolling.value = false;
},
});
const stylez = useAnimatedStyle(() => {
const size = isScrolling.value ? 80 : 40;
return {
transform: [
{
translateY: transY.value,
},
],
width: withSpring(size),
height: withSpring(size),
};
});
const windowHeight = Dimensions.get('window').height - headerHeight;
const height = Platform.OS === 'web' ? windowHeight : undefined;
return (
<View style={styles.container}>
<View style={[styles.half, { height }]}>
<Animated.View style={[styles.box, stylez]} />
</View>
<View style={[styles.half, { height }]}>
<Animated.ScrollView
style={styles.scroll}
scrollEventThrottle={1}
onScroll={scrollHandler}>
<View style={styles.placeholder} />
<View style={styles.placeholder} />
<View style={styles.placeholder} />
<View style={styles.placeholder} />
<View style={styles.placeholder} />
</Animated.ScrollView>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
},
half: {
flex: 1,
overflow: 'hidden',
},
scroll: {
flex: 1,
backgroundColor: 'yellow',
},
box: {
alignSelf: 'center',
backgroundColor: 'black',
},
placeholder: {
width: size,
height: size,
backgroundColor: 'brown',
marginVertical: 300,
},
});
export default ScrollExample;