我正在尝试使用expo在react-native中设置抖动检测,但很难找到从哪里开始。我已经研究过使用加速度计和陀螺仪,但是我不知道如何实现它们以进行抖动检测。
我已经尝试使用react-native-shake库,但是效果不是很好。
答案 0 :(得分:0)
这是一个没有外部库的基本概念示例。调整更新间隔和灵敏度以适合您的需求。
import { Accelerometer } from "expo-sensors";
const configureShake = onShake => {
// update value every 100ms.
// Adjust this interval to detect
// faster (20ms) or slower shakes (500ms)
Accelerometer.setUpdateInterval(100);
// at each update, we have acceleration registered on 3 axis
// 1 = no device movement, only acceleration caused by gravity
const onUpdate = ({ x, y, z }) => {
// compute a total acceleration value, here with a square sum
// you can eventually change the formula
// if you want to prioritize an axis
const acceleration = Math.sqrt(x * x + y * y + z * z);
// Adjust sensibility, because it can depend of usage (& devices)
const sensibility = 1.8;
if (acceleration >= sensibility) {
onShake(acceleration);
}
};
Accelerometer.addListener(onUpdate);
};
// usage :
const subscription = configureShake(acceleration => {
console.log("shake with acceleration " + acceleration);
});
// when you're done, don't forget to unsubscribe
Accelerometer.removeAllListeners();
有关Expo documentation的更多信息
此外,如果抖动是应用程序的重要功能,则还应让用户能够调整灵敏度,因为android手机具有各种传感器精度。