如何在长按手势回调方法里获取手指触摸点的坐标

如何在长按手势回调方法里获取手指触摸点的坐标

HarmonyOS
2024-03-17 14:25:51
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
okmwq

使用组合手势的顺序识别,当长按手势事件结束后触发拖动手势事件。在手势回调方法里获取event(GestureEvent类型)的fingerList(FingerInfo类型),获取到localX和localY数值,表示相对于当前组件元素原始区域左上角的坐标地址。可参考如下代码:

import { promptAction } from '@kit.ArkUI'; 
 
@Entry 
@Component 
struct CombinedExample { 
  @State count: number = 0; 
  private touchAreaRight: number = 0; 
  private touchAreaBottom: number = 0; 
  @State positionX: number = 0; 
  @State positionY: number = 0; 
  @State gestureEventInfo: string = ''; 
 
  build() { 
    Column() { 
      Row() { 
        Column() { 
          Text('+') 
            .fontSize(28) 
            .position({ x: this.positionX, y: this.positionY }) 
        } 
        .height(200) 
        .width('100%') 
        .backgroundColor("#F1F3F5") 
        .onAreaChange((oldValue: Area, newValue: Area) => { 
          this.touchAreaRight = newValue.width as number; 
          this.touchAreaBottom = newValue.height as number; 
        }) 
        .gesture( 
          // 以下组合手势为顺序识别,当长按手势事件未正常触发时则不会触发拖动手势事件 
          GestureGroup(GestureMode.Sequence, 
            LongPressGesture({ repeat: true }) 
              .onAction((event: GestureEvent) => { 
                if (event.repeat) { 
                  this.count++; 
                } 
              }), 
            PanGesture() 
              .onActionStart(() => { 
                promptAction.showToast({ message: "Pan start", duration: 1000 }); 
              }) 
              .onActionUpdate((event: GestureEvent) => { 
                for (let i = 0; i < event.fingerList.length; i++) { 
                  if (event.fingerList[i] == undefined 
                    || event.fingerList[i].localX < 0 
                    || event.fingerList[i].localY < 0 
                    || event.fingerList[i].localX > this.touchAreaRight 
                    || event.fingerList[i].localY > this.touchAreaBottom) { 
                    return; 
                  } 
                  this.positionX = event.fingerList[i].localX; 
                  this.positionY = event.fingerList[i].localY; 
                } 
                this.gestureEventInfo = "sequence gesture\n" + "LongPress onAction" + this.count 
                  + "\nX:" + this.positionX + "\nY:" + this.positionY; 
              }) 
              .onActionEnd(() => { 
                promptAction.showToast({ message: "Pan end", duration: 1000 }); 
              }) 
          ) 
            .onCancel(() => { 
              promptAction.showToast({ message: "取消", duration: 1000 }); 
            }) 
        ) 
      } 
      .padding(12) 
      .borderRadius(24) 
      .backgroundColor(Color.White) 
 
      Text(this.gestureEventInfo) 
        .fontSize(18) 
        .width('100%') 
        .textAlign(TextAlign.Start) 
        .padding({ left: 18, top: 30 }) 
    } 
    .height('100%') 
    .width('100%') 
    .backgroundColor("#F1F3F5") 
  } 
}
  • 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.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
分享
微博
QQ
微信
回复
2024-03-18 17:06:05


相关问题
如何可以获取组件中心坐标
1247浏览 • 1回复 待解决
如何在NAPI执行上层获取env
818浏览 • 1回复 待解决
HarmonyOS 点击事件方法
1057浏览 • 1回复 待解决
HarmonyOS 获取组件坐标方法
1005浏览 • 1回复 待解决
HarmonyOS onAreaChange方法问题
987浏览 • 1回复 待解决
HarmonyOS 触摸手势相关问题
564浏览 • 1回复 待解决
HarmonyOS 此方法如何转成vp
606浏览 • 1回复 待解决
HarmonyOS @Watch方法没有改变
646浏览 • 1回复 待解决