uni.vibrateShort(options)
使设备发生短时间的振动。
vibrateShort 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
| x | 4.41 | 5.08 | 5.08 | 5.08 | 5.08 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| options | VibrateShortOptions | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| |
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 | | type | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| | | 合法值 | 兼容性 | 描述 | | heavy | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| - | | medium | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| - | | light | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| - |
| | success | (result: VibrateShortSuccess) => void | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| | | fail | (result: VibrateShortFail) => void | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| | | complete | (result: VibrateShortSuccess | VibrateShortFail) => void | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| |
|
VibrateShortSuccess 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| errMsg | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| |
VibrateShortFail 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| errCode | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| |
| 合法值 | 兼容性 | 描述 | | 9001001 | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| - | | 9001002 | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| - | | 9001003 | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| - |
|
| errSubject | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| 统一错误主题(模块)名称 |
| data | any | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| 错误信息中包含的数据 |
| cause | Error | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError |
| errMsg | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| |
参见
uni.vibrateLong(options)
使设备发生长时间的振动。
vibrateLong 兼容性
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
| x | 4.41 | 5.08 | 5.08 | 5.08 | 5.08 |
参数
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
| options | VibrateLongOptions | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| |
|
VibrateLongSuccess 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| errMsg | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| |
VibrateLongFail 的属性值
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| errCode | number | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| |
| 合法值 | 兼容性 | 描述 | | 9001001 | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| - | | 9001002 | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| - | | 9001003 | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| - |
|
| errSubject | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| 统一错误主题(模块)名称 |
| data | any | 否 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| 错误信息中包含的数据 |
| cause | Error | 否 | - | - | 源错误信息,可以包含多个错误,详见SourceError |
| errMsg | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | x | - | - | - | - |
| |
参见
示例
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
该 API 不支持 Web,请运行 hello uni-app x 到 App 平台体验
扫码体验 示例源码如下,请查看 pre > code 标签中的内容
<template>
<!-- #ifdef APP -->
<scroll-view style="flex: 1;padding: 6px;">
<!-- #endif -->
<text class="uni-h2">振动</text>
<text class="notice">如下情况会导致无法振动:设备电量不足、静音模式、关闭了系统触感</text>
<text class="uni-h3">短振动 uni.vibrateShort</text>
<text class="margin-v">短振动支持 `light`、`medium`、`heavy` 三种力度类型。</text>
<button class="margin-v" @tap="callShort('light')">轻振动</button>
<button class="margin-v" @tap="callShort('medium')">中振动</button>
<button class="margin-v" @tap="callShort('heavy')">强振动</button>
<text class="uni-h3">长振动 uni.vibrateLong</text>
<button @tap="callLong">触发长振动</button>
<text class="uni-h4">最近日志:</text>
<text class="log-text">{{ statusText }}</text>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script setup lang="uts">
type ShortType = 'heavy' | 'medium' | 'light'
const statusText = ref('等待触发振动')
function updateStatus(message : string) : void {
statusText.value = message
}
function getShortTypeLabel(type : ShortType) : string {
switch (type) {
case 'light':
return '轻振动'
case 'medium':
return '中振动'
default:
return '强振动'
}
}
function callShort(type : ShortType) : void {
uni.vibrateShort({
type,
success: () => {
updateStatus(`${getShortTypeLabel(type)}调用成功`)
},
fail: (err) => {
updateStatus(`短振动调用失败:${err.errMsg}`)
}
})
}
function callLong() : void {
uni.vibrateLong({
success: () => {
updateStatus('长振动调用成功')
},
fail: (err) => {
updateStatus(`长振动调用失败:${err.errMsg}`)
}
})
}
</script>
<style>
.margin-v {
margin: 5px 0;
}
.log-text {
padding: 10px;
font-size: 14px;
border: 1px solid #ccc;
}
.notice {
color: #550000;
font-size: 14px;
font-style: italic;
}
</style>
通用类型
GeneralCallbackResult
| 名称 | 类型 | 必备 | 默认值 | 兼容性 | 描述 |
| errMsg | string | 是 | - | |
Web
|
微信小程序
|
Android
|
iOS
|
HarmonyOS
| | - | 4.41 | - | - | - |
| 错误信息 |