百度智能小程序 停止監(jiān)聽加速度數(shù)據(jù)

2020-09-05 14:29 更新

swan.stopAccelerometer

解釋:停止監(jiān)聽加速度數(shù)據(jù)。

方法參數(shù)

Object object

object 參數(shù)說明

屬性名類型必填默認值說明

success

Function

接口調(diào)用成功的回調(diào)函數(shù)

fail

Function

接口調(diào)用失敗的回調(diào)函數(shù)

complete

Function

接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會執(zhí)行)

示例 

在開發(fā)者工具中打開


圖片示例

代碼示例

<view class="wrap">
    <view class="page-top">
        <view class="page-top-text">{{title}}</view>
        <view class="page-top-line"></view>
    </view>
    <view class="page-body-canvas">
        <image mode="aspectFit" src="https://b.bdstatic.com/searchbox/icms/searchbox/img/gravity.png" rel="external nofollow"  class="page-body-ball"></image>
        <canvas class="page-body-ball" show="{{true}}" canvas-id="small-ball"></canvas>
    </view>
    <view class="page-body-xyz">
        <text class="page-body-title">X: {{x}}</text>
        <text class="page-body-title">Y: {{y}}</text>
        <text class="page-body-title">Z: {{z}}</text>
    </view>
    <view class="page-body-btn">
        <view class="middle-btn {{!isWatch ? '' : 'disabled'}}" bind:tap="startWatch">開始監(jiān)聽</view>
        <view class="middle-btn {{!isWatch ? 'disabled' : ''}}" bind:tap="stopWatch">停止監(jiān)聽</view >
    </view>
</view>
Page({
    data: {
        title: '傾斜手機即可移動下方小球',
        isWatch: true,
        x: 0,
        y: 0,
        z: 0
    },

    onReady() {
        this.position = {
            x: 151,
            y: 151,
            vx: 0,
            vy: 0,
            ax: 0,
            ay: 0
        };
        swan.onAccelerometerChange(res => {
            this.setData({
                x: res.x.toFixed(2),
                y: res.y.toFixed(2),
                z: res.z.toFixed(2)
            });
            this.position.ax = Math.sin(res.x * Math.PI / 2);
            this.position.ay = -Math.sin(res.y * Math.PI / 2);
        });
        this.interval = setInterval(() => {
            this.drawSmallBall();
        }, 20);
    },
    onShow() {
        let isWatch = this.data.isWatch;
        isWatch 
        ? swan.startAccelerometer()
        : swan.stopAccelerometer();
    },
    drawSmallBall() {
        let p = this.position;
        p.x = p.x + p.vx;
        p.y = p.y + p.vy;
        p.vx = p.vx + p.ax;
        p.vy = p.vy + p.ay;

        if (Math.sqrt(Math.pow(Math.abs(p.x) - 151, 2) + Math.pow(Math.abs(p.y) - 151, 2)) >= 115) {
            if (p.x > 151 && p.vx > 0) {
                p.vx = 0;
            }
            if (p.x < 151 && p.vx < 0) {
                p.vx = 0;
            }
            if (p.y > 151 && p.vy > 0) {
                p.vy = 0;
            }
            if (p.y < 151 && p.vy < 0) {
                p.vy = 0;
            }
        }
        let stx = this.createCanvasContext('small-ball');
        stx.beginPath(0);
        stx.arc(p.x, p.y, 15, 0, Math.PI * 2);
        stx.setFillStyle('#3388FF');
        stx.fill();
        stx.draw();
    },
    startWatch() {
        this.setData('isWatch', true);
        swan.startAccelerometer();
    },
    stopWatch() {
        this.setData('isWatch', false);
        swan.stopAccelerometer();
    },
    onHide() {
        clearInterval(this.interval);
        app.globalData.openParams = '';
    },
    onUnload() {
        clearInterval(this.interval);
    }
});


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號