This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo 设备运动
一个提供访问设备运动和方向传感器的库。
expo-sensors 中的 DeviceMotion 提供对设备运动和方向传感器的访问。所有数据都以穿过设备的三条轴来表示。按照竖屏方向:X 轴从左到右,Y 轴从下到上,Z 轴垂直穿过屏幕,从后到前。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
在 app 配置中进行配置
如果你在项目中使用 config plugins(Continuous Native Generation (CNG)),可以使用其内置的 config plugin 来配置来自 expo-sensor 的 DeviceMotion。该插件允许你配置各种无法在运行时设置、且需要构建新的应用二进制文件才会生效的属性。如果你的应用没有使用 CNG,那么你需要手动配置该库。
Example app.json with config plugin
Configurable properties
Are you using this library in an existing React Native app?
如果你没有使用 Continuous Native Generation (CNG),或者你是在手动使用原生 ios 项目,那么你需要在原生项目中配置 NSMotionUsageDescription 键,才能访问 DeviceMotion 统计信息:
API
import { DeviceMotion } from 'expo-sensors';
Constants
Type: number
Constant value representing standard gravitational acceleration for Earth (9.80665 m/s^2).
Classes
Type: Class extends DeviceSensor<DeviceMotionMeasurement>
A base class for subscribable sensors. The events emitted by this class are measurements
specified by the parameter type Measurement.
DeviceMotion Properties
DeviceMotion Methods
Subscribe for updates to the device motion sensor.
EventSubscriptionA subscription that you can call remove() on when you would like to unsubscribe the listener.
Checks user's permissions for accessing sensor.
Promise<PermissionResponse>Returns boolean which signifies if sensor has any listeners registered.
booleanYou should always check the sensor availability before attempting to use it.
Returns whether the accelerometer is enabled on the device.
On mobile web, you must first invoke DeviceMotion.requestPermissionsAsync() in a user interaction (i.e. touch event) before you can use this module.
If the status is not equal to granted then you should inform the end user that they may have to open settings.
On web this starts a timer and waits to see if an event is fired. This should predict if the iOS device has the device orientation API disabled in
Settings > Safari > Motion & Orientation Access. Some devices will also not fire if the site isn't hosted with HTTPS as DeviceMotion is now considered a secure API.
There is no formal API for detecting the status of DeviceMotion so this API can sometimes be unreliable on web.
Promise<boolean>A promise that resolves to a boolean denoting the availability of device motion sensor.
Asks the user to grant permissions for accessing sensor.
Promise<PermissionResponse>Interfaces
A subscription object that allows to conveniently remove an event listener from the emitter.
Types
Literal type: union
Permission expiration time. Currently, all permissions are granted permanently.
Acceptable values are: 'never' | number
Enums
权限
iOS
此库使用以下用途说明键: