swan.onAccelerometerChange
解释:监听加速度数据,频率依赖 swan.startAccelerometer 传递的interval,默认 5 次/秒。接口调用后会自动开始监听,可使用 swan.stopAccelerometer 停止监听。
方法参数
Function callback
callback参数说明
示例
在开发者工具中预览效果
扫码体验
请使用百度APP扫码
图片示例
代码示例
-
onReady() { this.position = { x: 151, y: 151, vx: 0, vy: 0, ax: 0, ay: 0 }; let self = this; swan.onAccelerometerChange(function (res) { self.setData({ x: res.x.toFixed(2), y: res.y.toFixed(2), z: res.z.toFixed(2) }); self.position.ax = Math.sin(res.x * Math.PI / 2); self.position.ay = -Math.sin(res.y * Math.PI / 2); }); }
