文档开发者中心
我的
|

ArkUI (方舟UI框架)

UI

HarmonyOS 5

HarmonyOS 使用gestureModifier时,如何同时绑定两个手势,比如同时绑定pan手势和pinch手势,当手指触摸到屏幕时,两个手势都可以响应?(API12+)

HarmonyOS 使用gestureModifier时,如何同时绑定两个手势,比如同时绑定pan手势和pinch手势,当手指触摸到屏幕时,两个手势都可以响应?(API12+)

点赞
收藏
回复
1
分享
举报
浏览44 发布于2026-04-02 11:25未知归属地
全部评论
最多点赞
最新发布
最早发布

可以通过创建手势组来实现。

参考demo:

收起
自动换行
深色代码主题
复制
class MyButtonModifier5 implements GestureModifier {
  supportDoubleTap: boolean = true;

  applyGesture(event: UIGestureEvent): void {
    if (this.supportDoubleTap) {
      event.addGesture(
        new PanGestureHandler({ fingers: 1, direction: PanDirection.All, distance: 5})
          .onActionStart((event: GestureEvent) => {
            console.log("zcb button pan " + JSON.stringify(event));
          })
          .tag("10"),
      )
      event.addGesture(
        new PanGestureHandler({ fingers: 1, direction: PanDirection.All, distance: 5})
          .onActionStart((event: GestureEvent) => {
            console.log("zcb button pan " + JSON.stringify(event));
          })
          .tag("11"),
      )
      event.addGesture(
        new PanGestureHandler({ fingers: 1, direction: PanDirection.All, distance: 5})
          .onActionStart((event: GestureEvent) => {
            console.log("zcb button pan " + JSON.stringify(event));
          })
          .tag("aaa"),
      )
      event.addGesture(
        new GestureGroupHandler({ mode: GestureMode.Parallel, gestures: [
          new GestureGroupHandler({ mode: GestureMode.Exclusive, gestures: [
            new GestureGroupHandler({ mode: GestureMode.Sequence, gestures: [
              new LongPressGestureHandler()
                .onAction((event: GestureEvent) => {
                  console.log("zcb button long press");
                })
                .tag("aaa"),
              new PanGestureHandler()
                .onActionStart((event: GestureEvent) => {
                  console.log("zcb button pan");
                })
                .tag("5"),
            ]}).tag("3"),
            new GestureGroupHandler({ mode: GestureMode.Exclusive, gestures: [
              new TapGestureHandler({ count: 1, fingers: 1 })
                .onAction((event: GestureEvent) => {
                  console.log("zcb button click Click");
                })
                .tag("aaa"),
              new TapGestureHandler({ count: 2, fingers: 1 })
                .onAction((event: GestureEvent) => {
                  console.log("zcb button double Click");
                })
                .tag("6"),
            ]}).tag("aaa"),
            new TapGestureHandler({ count: 1, fingers: 1 })
              .onAction((event: GestureEvent) => {
                console.log("zcb button click Click");
              })
              .tag("aaa"),
            new TapGestureHandler({ count: 2, fingers: 1 })
              .onAction((event: GestureEvent) => {
                console.log("zcb button double Click");
              })
              .tag("7"),
          ]}).onCancel(()=>{
            console.log("GestureGroupHandler onCancel");
          })
            .tag("2"),
          new TapGestureHandler({ count: 1, fingers: 1 })
            .onAction((event: GestureEvent) => {
              console.log("zcb button click Click");
            })
            .tag("8"),
          new TapGestureHandler({ count: 2, fingers: 1 })
            .onAction((event: GestureEvent) => {
              console.log("zcb button double Click");
            })
            .tag("9"),
        ]})
          .tag("1")
          .onCancel(()=>{
            console.log("GestureGroupHandler onCancel");
          })
      )
      event.removeGestureByTag("aaa");
    }
  }
}

@Entry
@Component
struct CompositeSceneTest {
  @State message: string = 'Hello World';
  @State modifier: MyButtonModifier5 = new MyButtonModifier5();

  build() {
    Row() {
      Column() {
        Column()
          .gestureModifier(this.modifier)
          .width(500)
          .height(500)
          .backgroundColor(Color.Blue)
      }
      .width('100%')
    }
    .height('100%')
  }
}
展开全部
1楼回复于2026-04-02 11:25 未知归属地
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。