on-accelerometer-change.vue 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap uni-common-mt">
  5. <!-- #ifdef APP-PLUS -->
  6. <view class="uni-btn-v">
  7. <button class="shake" @tap="shake">摇一摇</button>
  8. </view>
  9. <!-- #endif -->
  10. <view class="uni-btn-v">
  11. <button type="primary" @tap="watchAcce">监听设备的加速度变化</button>
  12. <button type="primary" @tap="stopAcce">停止监听设备的加速度变化</button>
  13. </view>
  14. <view class="uni-textarea uni-common-mt">
  15. <textarea class="acc-show" :value="value" />
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. data() {
  23. return {
  24. title: 'onAccelerometerChange',
  25. value: ''
  26. }
  27. },
  28. onUnload() {
  29. uni.stopAccelerometer();
  30. },
  31. methods: {
  32. //#ifdef APP-PLUS
  33. shake() {
  34. uni.navigateTo({
  35. url: '/platforms/app-plus/shake/shake'
  36. })
  37. },
  38. //#endif
  39. watchAcce() {
  40. uni.onAccelerometerChange((res) => {
  41. this.value = "监听设备的加速度变化:\n" + "X轴:" + res.x.toFixed(2) + "\nY轴:" + res.y.toFixed(2) +
  42. "\nZ轴:" + res.z.toFixed(2);
  43. })
  44. },
  45. stopAcce() {
  46. uni.stopAccelerometer()
  47. }
  48. }
  49. }
  50. </script>
  51. <style>
  52. .shake {
  53. background-color: #FFCC33;
  54. color: #ffffff;
  55. margin-bottom: 50rpx;
  56. }
  57. .uni-textarea .acc-show{
  58. height: 240rpx;
  59. }
  60. </style>