目录

震动

接口声明

在manifest.json文件的 features属性中增加如下配置。

{"name": "system.vibrator"}

导入模块

在调用接口页面的<script>部分增加如下配置。

import vibrator from '@system.vibrator'

或

var vibrator = require("@system.vibrator")

使用限制

限制条件

说明

适用终端

手机、平板

适用区域

全球

接口定义

接口

描述

vibrator.vibrate(OBJECT)

触发震动,持续1秒。

[h2]vibrator.vibrate(OBJECT)

描述

触发震动,持续1秒。

OBJECT参数

参数

类型

是否必填

说明

mode(1030+)

string

否

振动模式,默认为long。

  • long:表示长振动。
  • short:表示短振动。

实现时,long的时长定义为1000ms,short的时长定义为35ms。

示例代码

vibrator.vibrate({ mode: "short" });

Demo

<template>
  <!-- Only one root node is allowed in template. -->
  <div class="container">
    <div class="item-container">
      <input onclick="vibratelong" type="button" value="Long vibration" class="btn-blue"/>
    </div>
    <div class="item-container">
      <input onclick="vibrateshort" type="button" value="Short vibration" class="btn-blue"/>
    </div>
    <div class="item-container" if="{{info}}">
      <text class="content">{{info}}</text>
    </div>
  </div>
</template>

<style lang="sass">
.container {
   flex-direction: column;
   justify-content: center;
   align-items: center;
}
.item-container {
    margin-top: 50px;
    margin-right: 30px;
    margin-left: 30px;
    flex-direction: column;
  }
.btn-blue {
    background-color: #0faeff;
    color: #ffffff; 
    width:400px;
    height: 100px;
    font-size: 50px;
    border-radius:15px;
  }
.content { 
    border: 2px ;
    border-radius: 10px;
    text-align: center;
  }
</style>

<script>
  import vibrator from '@system.vibrator';
  module.exports = {
    data: {
      componentData: {},
      info:'',
      get_success:'',
      get_fail:''
    },
    onInit: function () {
      this.$page.setTitleBar({ text: 'vibrator'});
    },
    vibratelong(){
      var that = this;
      vibrator.vibrate({mode: 'long'});
      that.info = "long vibration is defined as lasting for 1000 ms"
      console.log("long vibration is defined as lasting for 1000 ms");
    },
     vibrateshort(){
       var that = this;
      vibrator.vibrate({mode: 'short'});
      that.info = "short vibration is defined as lasting for 35 ms"
      console.log("short vibration is defined as lasting for 35 ms");
    }
  }
</script>

效果图如下:

版本更新说明

版本

发布日期

描述

1030

2018-10-31

原有震动接口只支持1s长时震动的能力,为了支持短时震动,对vibrate接口扩展了mode参数以支持短时震动。