-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
93 lines (85 loc) · 2.49 KB
/
App.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
import React, { useState, useEffect, useRef } from "react";
import { StyleSheet, Text, ScrollView, View, SafeAreaView } from "react-native";
import { DeviceMotion, type DeviceMotionMeasurement } from "expo-sensors";
import { type Subscription } from "expo-sensors/build/Pedometer";
const TILT_THRESHOLD = 0.5;
const generateRandomColor = () => {
const randomColor = Math.floor(Math.random() * 16777215).toString(16);
return `#${randomColor}`;
};
export default function App() {
const scrollViewRef = useRef<ScrollView>(null);
const [subscription, setSubscription] = useState<Subscription | null>(null);
useEffect(() => {
DeviceMotion.setUpdateInterval(1000);
const subscribe = () => {
setSubscription(
DeviceMotion.addListener((motionData) => {
handleMotion(motionData);
})
);
};
const unsubscribe = () => {
subscription && subscription.remove();
setSubscription(null);
};
subscribe();
return () => unsubscribe();
}, []);
const handleMotion = (motionData: DeviceMotionMeasurement) => {
const { rotation } = motionData;
if (rotation.beta > TILT_THRESHOLD) {
//Possibly find a way to slow down the scroll speed
scrollViewRef.current?.scrollTo({ y: 0, animated: true });
console.log("scroll up");
} else if (rotation.beta < -TILT_THRESHOLD) {
scrollViewRef.current?.scrollToEnd({ animated: true });
console.log("scroll down");
} else {
//Should stop scrolling at current position
console.log("no scroll");
}
};
return (
<SafeAreaView style={styles.container}>
<Text
style={{
fontSize: 30,
fontWeight: "bold",
textAlign: "center",
marginTop: 20,
}}
>
Gravity Scroll
</Text>
<ScrollView ref={scrollViewRef} style={{ flex: 1 }}>
{Array.from({ length: 10000 }).map((_, index) => {
return (
<View
key={index}
style={{
height: 100,
width: "100%",
backgroundColor: generateRandomColor(),
justifyContent: "center",
}}
>
<Text style={styles.text}>{index}</Text>
</View>
);
})}
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
},
text: {
fontSize: 20,
fontWeight: "bold",
textAlign: "center",
},
});