Skip to content

x-vibrate-s 震动

设备震动,对齐 DCloud success / fail / complete 函数式调用。

兼容性

HarmonyiOSAndroidWEB微信小程序
支持支持支持部分支持支持

调用

ts
import { vibrate, vibrateShort, vibrateLong, XVibrateOptions } from "@/uni_modules/x-vibrate-s"

vibrate({
  duration: 50,
  type: "medium",
  success: (res) => {
    console.log(res.errMsg)
  },
  fail: (err) => {
    console.log(err.errCode, err.errMsg)
  }
} as XVibrateOptions)

vibrateShort({ type: "light" } as XVibrateOptions)
vibrateLong({} as XVibrateOptions)

旧写法 vibrator(50) 已移除,请改为 vibrate({ duration: 50 })

方法

名称说明
vibrate自定义时长震动,默认 15ms
vibrateShort短震动,约 15ms,type 生效
vibrateLong长震动,约 400ms

参数

XVibrateOptions

字段说明默认
duration时长 ms,仅 vibrate 使用,范围 1–1000015
typelight / medium / heavymedium
success成功-
fail失败-
complete结束,成功带结果,失败为 null-

错误码:1001 系统错误,1002 参数错误,1003 设备无震动器,1004 权限被拒绝,1008 当前平台不支持。

平台差异

Android

  • android.permission.VIBRATE(普通权限,无需运行时申请)。
  • API 21–25:Vibrator.vibrate(long),忽略 type
  • API 26+:VibrationEffect.createOneShot;有振幅控制时 light=64medium=160heavy=255
  • API 31+:通过 VibratorManager.getDefaultVibrator() 取马达,低版本仍走 VIBRATOR_SERVICE

iOS(deploymentTarget 12)

  • 短震动:UIImpactFeedbackGenerator(iOS 10+ 即有;本插件最低 12)。
  • impactOccurred(intensity:) 仅 iOS 13+。
  • 自定义时长 / 长震动:iOS 13+ 且 CHHapticEngine.supportsHaptics 时用连续触感;否则一次系统震动,时长无法精确控制。
  • 不再使用 Thread.sleep 循环挡线程。

HarmonyOS

  • 已去掉本地 HAR,直接使用 @kit.SensorServiceKitvibrator.startVibration
  • 需声明 ohos.permission.VIBRATE(普通权限,安装授予)。宿主 entry/module.json5 也应声明,不要只写在 HAR。
  • 按官方开发指导:type: 'time' + { id: 0, deviceId: -1, usage: 'alarm' } + callback。新系统 NAPI 会读 deviceId,缺了会 Vibrate time failed, ret:-1
  • 短震在鸿蒙最短 50ms(15ms 会被马达拒绝);type 不区分强度。失败时再试 deviceId: 0haptic.clock.timer
  • 错误:201→1004,401→1002,801→1003,14600101→1001;fail.errMsg 带原生错误码。

Web

  • navigator.vibrate。Safari iOS 等不支持时 fail 1008。忽略 type

微信小程序

  • 短震动走 wx.vibrateShort({ type })
  • 长震动或 duration >= 400wx.vibrateLong
  • 不支持精确自定义时长。

版本

版权归 https://xui.tmui.design 你不得修改及二次开发,仅供 TMUI4 会员商用使用。不得转给非 VIP 会员使用,一经查实数倍赔偿,并追究法律责任。

更新日志

1.1.4(2026-08-16)

  • 鸿蒙:VibrateAttribute 补 deviceId(系统日志 Can not find deviceId property 会导致 time 振动 ret:-1);短震最短 50ms;失败再试 haptic.clock.timer

1.1.3(2026-08-16)

  • 鸿蒙按官方开发指导重写:startVibration 使用 callback、id: 0usage: 'alarm'type: 'time';UTS/ETS 用 JSON 回传,避免错误码丢失成 1001 系统错误。

1.1.2(2026-08-16)

  • 鸿蒙震动:usage 不再用 unknown(触感开关关闭会 14600101→1001)。时长走 notification/alarm,短震预设先探测再回退 time。

1.1.1(2026-08-16)

  • 鸿蒙 fail.errMsg 改为必填字符串,避免 string | undefined

1.1.0(2026-08-16)

  • 改为 DCloud 函数式 API:vibrate / vibrateShort / vibrateLong,支持 success / fail / complete。
  • 鸿蒙去掉本地 HAR,按系统 SensorServiceKit.vibrator 重写。
  • Android 按 API 21/26/31 分支取马达与振幅;iOS 12 以下时长忽略,13+ 用 Core Haptics,去掉线程空转。

1.0.3(2025-07-25)

  • 兼容原生鸿蒙

1.0.2(2025-04-13)

  • IOS端现在支持指定震动频率.

1.0.1(2025-02-10)

  • 兼容微信

1.0.0(2024-06-01)

使用手机震动 安卓,ios支持。

最近更新