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

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

阅读 459
1 个回答

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. 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(()=>{

              });
            }
          })))
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进