codecamp

百度智能小程序 开始监听设备方向变化

swan.startDeviceMotionListening

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

解释:开始监听设备方向的变化。Web 态说明:由于浏览器对于 W3C 规范的实现差异,startDeviceMotionListening 功能在部分浏览器(比如 Safari 浏览器)下不可用,在功能不可用的情况下会执行开发者设置的 fail 回调

方法参数

Object object

object 参数说明

属性名类型必填默认值说明

interval

String

normal

监听设备方向的变化回调函数的执行频率

success

Function

接口调用成功的回调函数

fail

Function

接口调用失败的回调函数

complete

Function

接口调用结束的回调函数(调用成功、失败都会执行)

interval 的有效值

根据机型性能、当前 CPU 与内存的占用情况,interval 的设置与实际 swan.onAccelerometerChange() 回调函数的执行频率会有一些出入。

说明

game

适用于更新游戏的回调频率,在 20ms/次 左右

ui

适用于更新 UI 的回调频率,在 60ms/次 左右

normal

普通的回调频率,在 200ms/次 左右

示例 

在开发者工具中打开



图片示例

代码示例

<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);
    }
});


百度智能小程序 监听设备方向变化
百度智能小程序 停止监听设备方向变化
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

百度智能小程序开发文档

百度智能小程序 组件

百度智能小程序 地图

百度智能小程序 画布

百度智能小程序 API

百度智能小程序 界面

百度智能小程序 关注小程序引导组件

百度智能小程序 自定义组件

百度智能小程序 媒体

百度智能小程序 设备

百度智能小程序 拨打电话

百度智能小程序 内存警报

百度智能小程序 手机联系人

百度智能小程序 用户截屏事件

百度智能小程序 第三方平台

百度智能小程序 开放接口

百度智能小程序 百度收银支付

百度智能小程序 分包预下载

百度智能小程序 数据分析

百度智能小程序 服务端

百度智能小程序 云开发

百度智能小程序 初始化

百度智能小程序 云函数

百度智能小程序 服务端初始化

百度智能小程序 服务器获取上下文

百度智能小程序 服务端云函数

百度智能小程序 开发教程

百度智能小程序 功能开发

百度智能小程序 基本原理

百度智能小程序 小程序自动化

百度智能小程序 视频教程

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }