swan.onAccelerometerChange

解释:监听加速度数据,频率依赖 swan.startAccelerometerswan.onAccelerometerChange - 图1 传递的interval,默认 5 次/秒。接口调用后会自动开始监听,可使用 swan.stopAccelerometer 停止监听。

方法参数

Function callback

callback参数说明

属性名类型必填默认值说明
xNumberX 轴
yNumberY 轴
zNumberZ 轴

示例

在开发者工具中预览效果

扫码体验

swan.onAccelerometerChange - 图2请使用百度APP扫码

图片示例

swan.onAccelerometerChange - 图3

swan.onAccelerometerChange - 图4

swan.onAccelerometerChange - 图5

代码示例

  • 在 js 文件中
  1. onReady() {
  2. this.position = {
  3. x: 151,
  4. y: 151,
  5. vx: 0,
  6. vy: 0,
  7. ax: 0,
  8. ay: 0
  9. };
  10. let self = this;
  11. swan.onAccelerometerChange(function (res) {
  12. self.setData({
  13. x: res.x.toFixed(2),
  14. y: res.y.toFixed(2),
  15. z: res.z.toFixed(2)
  16. });
  17. self.position.ax = Math.sin(res.x * Math.PI / 2);
  18. self.position.ay = -Math.sin(res.y * Math.PI / 2);
  19. });
  20. }

错误码

Android

错误码说明
1001执行失败

iOS

错误码说明
202解析失败,请检查参数是否正确
1001设备不支持否正确