This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo DeviceMotion iconExpo DeviceMotion

提供对设备运动和方向传感器的访问的库。

Android
iOS
Web
Included in Expo Go
Recommended version:
~58.0.3

expo-sensors 提供的 DeviceMotion 可以访问设备的运动和方向传感器。所有数据都以穿过设备的三个轴来表示。根据竖屏方向:X 轴从左到右,Y 轴从下到上,Z 轴垂直穿过屏幕,从后到前。

安装

🌐 Installation

Terminal
- npx expo install expo-sensors
- yarn expo install expo-sensors
- pnpm expo install expo-sensors
- bun expo install expo-sensors

If you are installing this in an existing React Native app, make sure to install expo in your project.

应用配置中的配置

🌐 Configuration in app config

如果你在项目中使用配置插件(连续原生生成 (CNG)),可以通过其内置的配置插件从expo-sensor配置DeviceMotion。该插件允许你配置各种无法在运行时设置的属性,并需要构建新的应用二进制文件才能生效。如果你的应用不使用CNG,则需要手动配置该库。

🌐 You can configure DeviceMotion from expo-sensor using its built-in config plugin if you use config plugins in your project (Continuous Native Generation (CNG)). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect. If your app does not use CNG, then you'll need to manually configure the library.

Example app.json with config plugin

app.json
{ "expo": { "plugins": [ [ "expo-sensors", { "motionPermission": "Allow $(PRODUCT_NAME) to access your device motion." } ] ] } }

Configurable properties

NameDefaultDescription
motionPermission"Allow $(PRODUCT_NAME) to access your device motion"
Only for: 
iOS

A string to set the NSMotionUsageDescription permission message.

Are you using this library in an existing React Native app?

如果你没有使用连续原生生成(CNG),或者你是手动使用原生 iOS 项目,那么你需要在你的原生项目中配置 NSMotionUsageDescription 密钥以访问 DeviceMotion 统计数据:

🌐 If you're not using Continuous Native Generation (CNG) or you're using native ios project manually, then you need to configure NSMotionUsageDescription key in your native project to access DeviceMotion stats:

ios/[app]/Info.plist
<key>NSMotionUsageDescription</key> <string>Allow $(PRODUCT_NAME) to access your device motion</string>

应用接口

🌐 API

import { DeviceMotion } from 'expo-sensors';

No API data file found, sorry!

权限

🌐 Permissions

iOS

该库使用以下用法描述键:

🌐 The following usage description keys are used by this library:

Info.plist keyDescription

NSMotionUsageDescription

A message that tells the user why the app is requesting access to the device’s motion data.