虽然我仍然不确定正确的术语是什么,但我已经设法通过使用陀螺仪 API 并监控“Z 值”来获得我正在寻找的值。这是我的工作示例。(需要与expo一起运行)
import React from 'react';
import Expo, {
Gyroscope,
} from 'expo';
import { Text, TouchableOpacity, View } from 'react-native';
export default class Test extends React.Component {
constructor(props) {
super(props);
this.state = {
gyroscopeData: {
x: 0,
y: 0,
z: 0
},
}
}
componentDidMount() {
this._toggle();
}
componentWillUnmount() {
this._unsubscribe();
}
_toggle = () => {
if (this._subscription) {
this._unsubscribe();
} else {
this._subscribe();
}
}
_slow = () => {
Gyroscope.setUpdateInterval(1000);
}
_fast = () => {
Gyroscope.setUpdateInterval(16);
}
_subscribe = () => {
this._subscription = Gyroscope.addListener((result) => {
this.setState({gyroscopeData: result});
});
}
_unsubscribe = () => {
this._subscription && this._subscription.remove();
this._subscription = null;
}
render() {
let { x, y, z } = this.state.gyroscopeData;
return (
<View>
{/*<Text> x: {round(x)}</Text>*/}
{/*<Text> y: {round(y)}</Text>*/}
<Text> z: {z}</Text>
<View>
<TouchableOpacity onPress={this._toggle}>
<Text>Toggle</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this._slow}>
<Text>Slow</Text>
</TouchableOpacity>
<TouchableOpacity onPress={this._fast}>
<Text>Fast</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
function round(n) {
if (!n) {
return 0;
}
return Math.floor(n * 100) / 100;
}