HarmonyOS 有什么接口能实现手势或者是其他事件在.onTouch之前触发

现在onTouch触发的时候会在画布绘制路径,但是PinchGesture捏合手势做画布放大缩小的时候会先触发.onTouch事件,没办法做到放大缩小的时候不禁止绘制路径

HarmonyOS
3天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

在ontouch事件中,判断event.touches.length,有多个手指就return

// xxx.ets
@Entry
@Component
struct PinchGestureExample {
  @State scaleValue: number = 1
  @State pinchValue: number = 1
  @State pinchX: number = 0
  @State pinchY: number = 0
  @State text: string = ''
  @State eventType: string = ''

  aboutToDisappear(): void {

  }

  build() {
    Column() {
      Column() {
        Text('PinchGesture scale:\n' + this.scaleValue)
        Text('PinchGesture center:\n(' + this.pinchX + ',' + this.pinchY + ')')
      }
      .height(200)
      .width(300)
      .padding(20)
      .border({ width: 3 })
      .margin({ top: 100 })
      .scale({ x: this.scaleValue, y: this.scaleValue, z: 1 })
      // 两指捏合触发该手势事件
      .gesture(
        PinchGesture({ fingers: 2 })
          .onActionStart((event: GestureEvent) => {
            console.info('Pinch start')
          })
          .onActionUpdate((event: GestureEvent) => {
            if (event) {
              this.scaleValue = this.pinchValue * event.scale
              this.pinchX = event.pinchCenterX
              this.pinchY = event.pinchCenterY
            }
          })
          .onActionEnd((event: GestureEvent) => {
            this.pinchValue = this.scaleValue
            console.info('Pinch end')
          })
      )
      .onTouch((event?: TouchEvent) => {
        if(event){
          if (event.touches.length > 1) {
            return
          }
          if (event.type === TouchType.Down) {
            this.eventType = 'Down'
          }
          if (event.type === TouchType.Up) {
            this.eventType = 'Up'
          }
          if (event.type === TouchType.Move) {
            this.eventType = 'Move'
          }
          this.text = 'TouchType:' + this.eventType + '\nDistance between touch point and touch element:\nx: '
            + event.touches[0].x + '\n' + 'y: ' + event.touches[0].y + '\nComponent globalPos:('
            + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
            + event.target.area.width + '\nheight:' + event.target.area.height
        }
      })
      Text(this.text)
    }.width('100%')
  }
}
分享
微博
QQ
微信
回复
3天前
相关问题
如何拦截onTouch事件传递
717浏览 • 1回复 待解决
HarmonyOS 如何实现手势密码功能
537浏览 • 1回复 待解决
onTouch事件是否可以判断滑动方向
2042浏览 • 1回复 待解决
ArkTS runtime跟之前的maple什么区别
1836浏览 • 1回复 待解决