HarmonyOS Vibration如何实现长按震动效果

HarmonyOS
2024-12-25 08:26:28
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

startVibration

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-vibrator-V5#vibratorstartvibration9参考代码:

  1. src/main/module.json5下添加震动权限
"requestPermissions": [
{
  "name": "ohos.permission.VIBRATE"
}
]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  1. page.ets
import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

@Entry
@Component
export struct TestLongPressVibrationPage {
  @State color: Color = Color.Red
  flag: boolean = true;
  build() {
    Column() {
      Text('震动')
        .backgroundColor(this.color)
        .margin({top:200})
        .onClick(() => {
          this.color = Color.Blue
          vibrator.startVibration({
            type: 'time',
            duration: 100,
          }, {
            id: 0,
            usage: 'touch'
          }, (error: BusinessError) => {})
        })
    }
    .width('100%')
    .height('100%')
    .gesture(
      GestureGroup(GestureMode.Exclusive,
        LongPressGesture({ repeat: true })
          .onAction((event?: GestureEvent) => {
            console.info(`ah_长按`);
            if (event && event.repeat) {
              console.info(`ah_长按判断`);
              vibrator.startVibration({
                type: 'time',
                duration: 100,
              }, {
                id: 0,
                usage: 'touch'
              }).then(()=>{

              });
            }
          })))
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
分享
微博
QQ
微信
回复
2024-12-25 11:22:15


相关问题
长按实现各类振动效果
1599浏览 • 1回复 待解决
HarmonyOS 实现按钮长按动画效果
1116浏览 • 1回复 待解决
HarmonyOS 手机震动接口调用无效果
1437浏览 • 1回复 待解决
如何去掉div长按的灰色效果
3105浏览 • 1回复 待解决
HarmonyOS 如何震动
962浏览 • 1回复 待解决
HarmonyOS 如何实现长按点击功能
924浏览 • 1回复 待解决
HarmonyOS 如何使用代码触发震动
538浏览 • 1回复 待解决
如何开启手机的震动功能?
1169浏览 • 1回复 待解决
HarmonyOS 震动开发
782浏览 • 1回复 待解决
HarmonyOS vibrator如何自定义震动时长
567浏览 • 1回复 待解决
HarmonyOS 控件长按事件怎么实现
549浏览 • 1回复 待解决
HarmonyOS 如何实现粒子效果
647浏览 • 1回复 待解决
HarmonyOS 如何实现阴影效果
788浏览 • 1回复 待解决
HarmonyOS现在支持震动反馈吗?
1144浏览 • 1回复 待解决