swan.stopDeviceMotionListening

基础库 3.30.2 开始支持,低版本需做兼容处理。

解释:停止监听设备方向的变化。

# 方法参数

Object object

# object 参数说明

属性名 类型 必填 默认值 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

# 示例

扫码体验
重新加载
请使用百度APP扫码

# 图片示例

# 代码示例

    <view class="wrap">
        <view class="card-area">
            <view class="list-area border-bottom" s-for="item in infoList">
                <text class="list-item-key-4">{{item.chineseName}}</text>
                <text class="list-item-value">{{item.value}}</text>
            </view>
            <view class="button-group">
                <button bind:tap="stopDeviceMotionListening" disabled="{{!disabled}}" type="primary" hover-stop-propagation="true">停止监听</button>
                <button bind:tap="startDeviceMotionListening" disabled="{{disabled}}" type="primary" hover-stop-propagation="true">开始监听</button>
            </view>
        </view>
    </view>
    
    Page({
        data: {
            infoList: [{
                engName: 'alpha',
                chineseName: 'z轴角度',
                value: ''
            }, {
                engName: 'beta',
                chineseName: 'x轴角度',
                value: ''
            }, {
                engName: 'gamma',
                chineseName: 'y轴角度',
                value: ''
            }],
            disabled: false
        },
        startDeviceMotionListening() {
            swan.startDeviceMotionListening({
                interval: 'ui',
                success: () => {
                    this.setData('disabled', !this.data.disabled);
                    console.log('startDeviceMotionListening success');
                    this.onDeviceMotionChange();
                },
                fail: err => {
                    console.log('startDeviceMotionListening fail', err);
                }
            });
        },
        onDeviceMotionChange() {
            swan.onDeviceMotionChange(res => {
                console.log('onDeviceMotionChange', res);
                this.updateInfoList(res);
            });
        },
        stopDeviceMotionListening() {
            swan.stopDeviceMotionListening({
                success: () => {
                    this.setData('disabled', !this.data.disabled);
                    console.log('stopDeviceMotionListening success');
                },
                fail: err => {
                    console.log('stopDeviceMotionListening fail', err);
                }
            });
        },
        updateInfoList(res) {
            let infoList = this.data.infoList;
            for (let item of infoList) {
                if (!res[item.engName]) {
                    item.value = '暂无';
                } else {
                    item.value = res[item.engName].toFixed(2);
                }
            }
            this.setData('infoList', infoList);
        }
    });