-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
163 lines (143 loc) · 4.58 KB
/
App.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { StyleSheet, Text, View, TouchableOpacity
} from 'react-native';
import React, { useState, useEffect, useCallback, useMemo,useRef } from 'react';
import Animated, {
useAnimatedScrollHandler,
useSharedValue,
useAnimatedStyle,
useAnimatedRef,
scrollTo,
withSpring,
} from 'react-native-reanimated';
import {
PanGestureHandler,
GestureHandlerRootView,
GestureDetector,
Gesture
} from 'react-native-gesture-handler';
export default function App() {
const scrollY = useSharedValue(0);
const animatedScrollViewRef = useAnimatedRef();
const previousValue = useSharedValue(0);
const secondPreviousValue = useSharedValue(0);
const onChangeRef = useRef("scroll");
const onScroll = useAnimatedScrollHandler((event) => {
const currentScrollY = event.contentOffset.y;
if(onChangeRef.current === "scroll") {
// scrollTo(animatedScrollViewRef, 0, secondPreviousValue.value, true);
onChangeRef.current = "scrollTo";
console.log("scrollTo");
} else if(onChangeRef.current === "end") {
secondPreviousValue.value = previousValue.value;
previousValue.value = scrollY.value;
scrollY.value = currentScrollY;
onChangeRef.current = "scroll";
console.log("scroll");
} else {
onChangeRef.current = "end";
console.log("end");
}
// const isNearSecondPreviousValue = Math.abs(currentScrollY - secondPreviousValue.value) < 1;
// const isNearPreviousValue = Math.abs(currentScrollY - previousValue.value) < 1;
// console.log("currentScrollY", currentScrollY, previousValue.value, secondPreviousValue.value);
// if(isNearSecondPreviousValue) {
// // console.log("ok");
// // scrollTo(animatedScrollViewRef, 0, previousValue.value, true);
// } else if (isNearPreviousValue) {
// console.log("ok", secondPreviousValue.value);
// scrollTo(animatedScrollViewRef, 0, secondPreviousValue.value, true);
// } else {
// secondPreviousValue.value = previousValue.value;
// previousValue.value = scrollY.value;
// scrollY.value = currentScrollY;
// }
}, [animatedScrollViewRef]);
const scrollViewStyle = useAnimatedStyle(() => {
return {
transform: [{ translateY: 300 -scrollY.value }],
width: 300,
};
});
const contentStyle = useAnimatedStyle(() => {
return {
transform: [{ translateY: 0 }],
};
},[]);
const panRef = useSharedValue(0);
const [disable, setDisable] = useState(false);
const pan = Gesture.Pan().onStart(() => {
panRef.value = scrollY.value;
console.log("start", panRef.value);
}).onChange((event) => {
console.log("change", panRef.value);
scrollY.value = panRef.value - event.translationY;
scrollTo(animatedScrollViewRef, 0, scrollY.value, false);
}).onEnd(() => {
scrollY.value = withSpring(scrollY.value + 10);
});
return (
<View style={styles.container}>
<GestureHandlerRootView style={{ flex: 1 }}>
<GestureDetector
gesture={pan}
>
<Animated.ScrollView
onScroll={onScroll}
scrollEventThrottle={16}
style={scrollViewStyle}
ref={animatedScrollViewRef}
onLayout={() => {
console.log('Layout event', Object.keys( animatedScrollViewRef.current));
// Returns a reference to the component
const component = animatedScrollViewRef.current;
}}
>
{Array.from({ length: 100 }).map((_, i) => (
<Text key={i}>{i}</Text>
))}
</Animated.ScrollView>
</GestureDetector>
</GestureHandlerRootView>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
// import React from 'react';
// import { StyleSheet, View } from 'react-native';
// // import Animated, { useAnimatedRef } from 'react-native-reanimated';
// export default function App() {
// const animatedRef = useAnimatedRef();
// return (
// <View style={styles.container}>
// <Animated.View
// ref={animatedRef}
// style={styles.box}
// onLayout={() => {
// console.log('Layout event', animatedRef.current);
// // Returns a reference to the component
// const component = animatedRef.current;
// }}
// />
// </View>
// );
// }
// const styles = StyleSheet.create({
// container: {
// flex: 1,
// alignItems: 'center',
// },
// box: {
// height: 100,
// width: 100,
// backgroundColor: '#b58df1',
// borderRadius: 20,
// marginVertical: 64,
// },
// });