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

2020-09-05 14:32 更新

swan.stopCompass

解釋:停止監(jiān)聽羅盤數(shù)據(jù)。

方法參數(shù)

Object object

object 參數(shù)說明

屬性名類型必填默認(rèn)值說明

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="card-area">
    <view class="wrap">
        <view class="page-top">
            <view class="tip-week">{{title}}</view>
        </view>
        <view class="direction">
            <view class="bg-compass-line"></view>
            <view class="bg-compass" style="transform: rotate(-{{direction}}deg)">
                <view class="num num0" style="transform: rotate({{direction}}deg)">0</view>
                <view class="num num90" style="transform: rotate({{direction}}deg)">90</view>
                <view class="num num180" style="transform: rotate({{direction}}deg)">180</view>
                <view class="num num270" style="transform: rotate({{direction}}deg)">270</view>
            </view>
            <div class="cg-wrap">
                <div class="circle-left-wrap"><div class="circle-left" style="transform:rotate({{leftdeg}}deg);transform-origin:50% 50%"></div></div>
                <div class="circle-right-wrap"><div class="circle-right" style="transform:rotate({{rightdeg}}deg);transform-origin:50% 50%"></div></div>
                <div class="mask">
                    <view class="direction-num">
                        <text>{{direction}}</text>
                        <text class="direction-degree"></text>
                    </view>
                </div>
            </div>
        </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>
</view>
Page({
    data: {
        title: '旋轉(zhuǎn)手機(jī)即可獲取方位信息',
        direction: 0,
        isWatch: true,
        leftdeg: 0,
        rightdeg: 0
    },

    onReady() {
        swan.onCompassChange(res => {
            this.setData({
                direction: parseInt(res.direction, 10)
            });
            this.updateProgress(res.direction);
        });
    },
    onShow() {
        let isWatch = this.data.isWatch;
        isWatch ? swan.startCompass() : swan.stopCompass();
    },
    startWatch() {
        this.setData('isWatch', true);
        swan.startCompass();
    },
    stopWatch() {
        this.setData('isWatch', false);
        swan.stopCompass();
    },
    updateProgress(direction) {
        let leftdeg;
        let rightdeg;
        let deg = +direction;
        if (deg > 180) {
            leftdeg = 180;
            rightdeg = 180 - deg;
        }
        else {
            leftdeg = -deg;
            rightdeg = 0;
        }
        this.setData({
            leftdeg,
            rightdeg
        });
    }
});


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號