-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathAnimatedSensorExample.tsx
50 lines (47 loc) · 1.14 KB
/
AnimatedSensorExample.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
import React from 'react';
import Animated, {
useAnimatedStyle,
useAnimatedSensor,
SensorType,
} from 'react-native-reanimated';
import { View, Button, StyleSheet } from 'react-native';
export default function AnimatedStyleUpdateExample() {
const animatedSensor = useAnimatedSensor(SensorType.GRAVITY);
const style = useAnimatedStyle(() => {
const { x, y } = animatedSensor.sensor.value;
return {
transform: [{ translateX: x * 5 }, { translateY: y * 5 }],
};
});
return (
<View style={componentStyle.container}>
<Button
title={'log data'}
onPress={() => console.log(animatedSensor.sensor.value)}
/>
<Animated.View style={[componentStyle.rect]}>
<Animated.View style={[componentStyle.square, style]} />
</Animated.View>
</View>
);
}
const componentStyle = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
square: {
width: 10,
height: 10,
backgroundColor: 'red',
position: 'absolute',
left: 90,
top: 90,
},
rect: {
width: 200,
height: 200,
backgroundColor: 'black',
},
});