加速计

最近更新时间:2023-10-20 15:19:22

我的收藏

starAccelerometer

该 API 使用方法为 wx.startAccelerometer(Object object)
注意:
根据机型性能、当前 CPU 与内存的占用情况,interval的设置与实际 wx.onAccelerometerChange 回调函数的执行频率会有一些出入。
功能说明:开始监听加速度数据。
参数及说明:Object object。
属性
类型
默认值
必填
说明
interval
string
normal
监听加速度数据回调函数的执行频率
success
function
-
接口调用成功的回调函数
fail
function
-
接口调用失败的回调函数
complete
function
-
接口调用结束的回调函数(调用成功、失败都会执行)
object.interval 的合法值。
说明
game
适用于更新游戏的回调频率,在20ms/次 左右
ui
适用于更新 UI 的回调频率,在60ms/次 左右
normal
普通的回调频率,在200ms/次 左右
示例代码:
wx.startAccelerometer({
interval: 'game'
})

stopAccelerometer

该 API 使用方法为 wx.stopAccelerometer(Object object)
功能说明:停止监听加速度数据。
参数及说明:Object object。
属性
类型
默认值
必填
说明
success
function
-
接口调用成功的回调函数
fail
function
-
接口调用失败的回调函数
complete
function
-
接口调用结束的回调函数(调用成功、失败都会执行)
示例代码:
wx.stopAccelerometer()

onAccelerometerChange

该 API 使用方法为 wx.onAccelerometerChange(function listener)
功能说明:监听加速度数据事件。频率根据 wx.startAccelerometer() 的 interval 参数。可使用 wx.stopAccelerometer()停止监听。
参数及说明:function callback。
加速度数据事件的回调函数:
属性
类型
说明
x
number
X 轴
y
number
Y 轴
z
number
Z 轴
示例代码:
wx.onAccelerometerChange(function (res) {
console.log(res.x)
console.log(res.y)
console.log(res.z)
})

offAccelerometerChange

该 API 使用方法为 wx.offAccelerometerChange(function listener)
功能说明:移除加速度数据事件的监听函数。
参数及说明:function listener,onAccelerometerChange 传入的监听函数。不传此参数则移除所有监听函数。
示例代码:
const listener = function (res) { console.log(res) }

wx.onAccelerometerChange(listener)
wx.offAccelerometerChange(listener) // 需传入与监听时同一个的函数对