startDeviceMotionListening
功能描述
开始监听设备方向的变化。
需引入
BaseKit
,且在>=2.3.2
版本才可使用
使用
Ray 中使用
import { startDeviceMotionListening } from '@ray-js/ray'
startDeviceMotionListening({ ... })
原生小程序中使用
ty.startDeviceMotionListening({ ... })
请求参数
Object object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
interval | enum DeviceMotionInterval | DeviceMotionInterval.normal | 否 | 监听加速度数据回调函数的执行频率 |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 | |
complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行) |
返回结果
success
void
fail
属性 | 类型 | 说明 |
---|---|---|
errorMsg | string | 插件错误信息 |
errorCode | string | 错误码 |
引用对象
enum
DeviceMotionInterval
枚举值 | 描述 |
---|---|
game | 适用于更新游戏的回调频率,在 20ms/次 左右 |
ui | 适用于更新 UI 的回调频率,在 60ms/次 左右 |
normal | 普通的回调频率,在 200ms/次 左右 |
代码示例
请求示例
// Ray调用方式
import React, { useEffect, useState } from 'react'
import {
Button,
Text,
View,
offDeviceMotionChange,
onDeviceMotionChange,
startDeviceMotionListening,
stopDeviceMotionListening,
} from '@ray-js/ray'
import { useThrottleFn } from 'ahooks'
export default function Demo() {
const [data, setData] = useState<any>()
const listener = (data: any) => {
console.log('onDeviceMotionChange', data)
setData(data)
}
const [isListening, setIsListening] = useState(false)
const { run } = useThrottleFn(listener, { wait: 1000 })
useEffect(() => {
if (isListening) {
onDeviceMotionChange(run)
} else {
offDeviceMotionChange(run)
}
}, [isListening])
return (
<View style={{ width: '100%', height: '100%', padding: '20rpx' }}>
<Button
onClick={() => {
startDeviceMotionListening({
success: (res) => {
console.log('startDeviceMotionListening success', res)
setIsListening(true)
},
})
}}
>
startDeviceMotionListening
</Button>
<Button
onClick={() => {
stopDeviceMotionListening({
success: () => {
console.log('stopDeviceMotionListening success')
setIsListening(false)
},
})
}}
>
stopDeviceMotionListening
</Button>
<Text
style={{
color: '#333',
wordBreak: 'break-all',
whiteSpace: 'pre-wrap',
}}
>
{JSON.stringify(data)}
</Text>
</View>
)
}
// 原生调用方式
ty.startDeviceMotionListening({
success: console.log,
fail: console.error
});
ty.stopDeviceMotionListening({
success: console.log,
fail: console.error
});
ty.onDeviceMotionChange(console.log);
错误码
错误码 | 错误描述 |
---|---|
10001 | Sensor initialization fail |
此页内容是否对您有帮助?
是
意见反馈