uniapp 震动功能实现

项目场景:

提示:这里简述项目相关背景:

在项目中有时候需要一些功能,比如震动


描述

提示:这里描述项目中遇到的问题:

移动应用中,震动反馈是提升用户体验的重要方式。uniapp 提供了两种震动方式:

  • 短震动:轻微的触感反馈
  • 长震动:较强的震动反馈

分析:

提示:这里填写问题的分析:

1. 短震动 (uni.vibrateShort)

  • 作用:触发较短时间的震动
  • 震动时长:约 15ms
  • 适用场景:按钮点击、操作反馈等轻量级交互
javascript 复制代码
uni.vibrateShort(options: UniNamespace.VibrateShortOptions)

参数说明:

javascript 复制代码
interface VibrateShortOptions {
  success?: () => void  // 接口调用成功的回调函数
  fail?: (err: any) => void  // 接口调用失败的回调函数
  complete?: () => void  // 接口调用结束的回调函数(调用成功、失败都会执行)
}

2. 长震动 (uni.vibrateLong)

  • 作用:触发较长时间的震动
  • 震动时长:约 400ms
  • 适用场景:游戏结束、重要提示等需要明显震动反馈的场合
javascript 复制代码
uni.vibrateLong(options: UniNamespace.VibrateLongOptions)

参数说明:

javascript 复制代码
interface VibrateLongOptions {
  success?: () => void  // 接口调用成功的回调函数
  fail?: (err: any) => void  // 接口调用失败的回调函数
  complete?: () => void  // 接口调用结束的回调函数(调用成功、失败都会执行)
}

3:平台差异说明

| API | App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 |

|-----|-----|----| ---------- | ------------ | ---------- |

| vibrateShort | √ | x | √ | √ | √ |

| vibrateLong | √ | x | √ | √ | √ |

4:注意事项

权限要求:

  • App 平台需要在 manifest.json 中配置 vibrate 权限
  • 部分平台可能需要用户授权
  • 使用场景:
  • 短震动适合用于轻量级的交互反馈
  • 长震动建议用于重要提示,避免过度使用影响用户体验
  • 兼容性处理:
  • 建议添加错误处理逻辑
  • 在不支持的平台上需要提供替代方案

实例方案:

提示:这里填写该问题的具体解决方案:

1. 创建震动工具函数

useVibrate.ts 文件

javascript 复制代码
/**
 * 震动工具函数
 */
export function useVibrate() {
  // 短震动
  const vibrateShort = () => {
    uni.vibrateShort({
      success: () => {
        console.log('短震动成功')
      },
      fail: (err) => {
        console.error('短震动失败:', err)
      }
    })
  }
 
  // 长震动
  const vibrateLong = () => {
    uni.vibrateLong({
      success: () => {
        console.log('长震动成功')
      },
      fail: (err) => {
        console.error('长震动失败:', err)
      }
    })
  }
 
  return {
    vibrateShort,
    vibrateLong
  }
}

2:页面中使用

javascript 复制代码
<template>
  <view class="container">
    <button @click="handleShortVibrate">短震动</button>
    <button @click="handleLongVibrate">长震动</button>
  </view>
</template>
 
<script setup lang="ts">
import { useVibrate } from '@/composables/useVibrate'
 
const { vibrateShort, vibrateLong } = useVibrate()
 
const handleShortVibrate = () => {
  vibrateShort()
}
 
const handleLongVibrate = () => {
  vibrateLong()
}
</script>
相关推荐
三声三视几秒前
uni-app 鸿蒙端传参变成 [object Object]?顺着源码追到 ArkTS router 底层才搞明白
人工智能·ai·uni-app·aigc·ai编程·harmonyos
小徐_23338 小时前
Open Wot 1.0.5 发布:让 AI 接入 wot-ui,只需要两条命令
前端·uni-app·ai编程
AI多Agent协作实战派11 小时前
AI多Agent协作系统实战(二十二):从6列到12列——任务监控报告的进化之路
java·人工智能·uni-app·bug
小杨小杨、努力变强!13 小时前
VS Code运行HBuilder X中的uni-app项目
vscode·uni-app·uni-app run
码兄科技1 天前
实战:基于Spring Boot + UniApp的地理信息小程序开发
spring boot·后端·uni-app
2501_916007471 天前
iOS和macOS应用程序性能分析和优化工具使用综合指南
android·macos·ios·小程序·uni-app·iphone·webview
2501_916007472 天前
深入理解HTTPS对称与非对称加密机制及Charles抓包实践
网络协议·http·ios·小程序·https·uni-app·iphone
小徐_23332 天前
AI 写 wot-ui 总在猜 API?我们把 Skills、MCP 和 CLI 都配好了
前端·uni-app·ai编程
Liu.7742 天前
uni-app 组件 uni-easyinput 常见 Bug 及解决方案
uni-app·bug
小徐_23333 天前
uni-app 项目别再从零搭了!3 个 Wot UI 起手模板怎么选?
前端·uni-app