微信开发文档之前是有摇一摇的API的,但是后来好像是废弃了,下面这段代码主要是用的微信开发文档里面的加速计里边的两个API实现的。
// 开始监听加速度数据。
wx.startAccelerometer({
interval: 'game',
success:function(){
// 监听加速度数据事件。频率根据 wx.startAccelerometer() 的 interval 参数, 接口调用后会自动开始监听。
wx.onAccelerometerChange(function(res){
// res.x、res.y、res.z设备偏移量
if(res.x > 3 || res.y > 3 || res.z > 3){
wx.showToast({
title: '摇一摇成功!',
icon: 'success',
duration: 3000
})
}
})
}
})
wx.startAccelerometer(Object object)
属性 | 类型 | 默认值 | 说明 |
---|---|---|---|
interval | string | normal | 监听加速度数据回调函数的执行频率 |
success | function | - | 接口调用成功的回调函数 |
fail | function | - | 接口调用失败的回调函数 |
complete | function | - | 接口调用结束的回调函数(调用成功、失败都会执行) |
interval参数的值
合法值 | 说明 |
---|---|
game | 适用于更新游戏的回调频率,在 20ms/次 左右 |
ui | 适用于更新 UI 的回调频率,在 60ms/次 左右 |
normal | 普通的回调频率,在 200ms/次 左右 |
wx.onAccelerometerChange(function listener)
function返回值
属性 | 类型 | 说明 |
---|---|---|
x | number | X轴 |
y | number | Y轴 |
z | number | Z轴 |