-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathScrollToExample.tsx
174 lines (158 loc) · 4.13 KB
/
ScrollToExample.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
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
164
165
166
167
168
169
170
171
172
173
174
import React from 'react';
import { StyleSheet, View, Text, Platform } from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';
import Animated, {
useSharedValue,
useAnimatedStyle,
useAnimatedGestureHandler,
scrollTo,
useDerivedValue,
useAnimatedRef,
} from 'react-native-reanimated';
import {
PanGestureHandler,
PanGestureHandlerGestureEvent,
} from 'react-native-gesture-handler';
const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const indices = [0, 1, 2, 3];
const range = [0, 9999];
const dotSize = 40;
function ScrollToScreen(): React.ReactElement {
const progress = useSharedValue(0);
const number = useDerivedValue(() => {
const val = range[0] + Math.round(progress.value * (range[1] - range[0]));
return val;
});
return (
<SafeAreaView>
<View style={{ alignItems: 'center' }}>
<NumberDisplay number={number} />
<Text>move dot</Text>
<View>
<ProgressBar progress={progress} />
</View>
</View>
</SafeAreaView>
);
}
function getDigit(number: Animated.SharedValue<number>, i: number) {
return useDerivedValue(() => {
return Math.floor(number.value / 10 ** i) % 10;
});
}
function NumberDisplay({ number }: { number: Animated.SharedValue<number> }) {
return (
<View style={{ height: 400, width: 200 }}>
<View
style={{
flexDirection: 'row-reverse',
justifyContent: 'space-between',
alignItems: 'center',
}}>
{indices.map((i) => {
return <Digit digit={getDigit(number, i)} key={i} />;
})}
</View>
</View>
);
}
function Digit({ digit }: { digit: Animated.SharedValue<number> }) {
const aref = useAnimatedRef<Animated.ScrollView>();
useDerivedValue(() => {
if (Platform.OS === 'web') {
if (aref && aref.current) {
aref.current.getNode().scrollTo({ y: digit.value * 200 });
}
} else {
// TODO fix this
scrollTo(aref, 0, digit.value * 200, true);
}
});
return (
<View
style={{ height: 200, width: Platform.OS === 'web' ? 50 : undefined }}>
<Animated.ScrollView ref={aref}>
{digits.map((i) => {
return (
<View
style={{
height: 200,
alignItems: 'center',
flexDirection: 'row',
}}
key={i}>
<Text style={{ fontSize: 30 }}>{i}</Text>
</View>
);
})}
</Animated.ScrollView>
</View>
);
}
function ProgressBar({ progress }: { progress: Animated.SharedValue<number> }) {
const x = useSharedValue(0);
const max = useSharedValue(0);
const handler = useAnimatedGestureHandler<
PanGestureHandlerGestureEvent,
{ x: number }
>({
onStart: (_, ctx) => {
ctx.x = x.value;
},
onActive: (e, ctx) => {
let newVal = ctx.x + e.translationX;
newVal = Math.min(max.value, newVal);
newVal = Math.max(0, newVal);
x.value = newVal;
},
onEnd: (_) => {
progress.value = x.value / max.value;
},
});
const stylez = useAnimatedStyle(() => {
return {
transform: [{ translateX: x.value }],
};
});
const barStyle = useAnimatedStyle(() => {
return {
width: max.value,
};
});
return (
<View
style={{ height: 100, paddingRight: 80, paddingLeft: 40, width: 300 }}>
<View
onLayout={(e) => {
max.value = e.nativeEvent.layout.width;
}}>
<Animated.View
style={[
{
backgroundColor: 'black',
height: 2,
marginRight: 20,
transform: [
{ translateY: dotSize / 2 + 1 },
{ translateX: dotSize / 2 },
],
},
barStyle,
]}
/>
<PanGestureHandler onGestureEvent={handler}>
<Animated.View style={[styles.dot, stylez]} />
</PanGestureHandler>
</View>
</View>
);
}
const styles = StyleSheet.create({
dot: {
borderRadius: 100,
backgroundColor: 'black',
width: dotSize,
height: dotSize,
},
});
export default ScrollToScreen;