ArkTS开发系列之事件(2.8.2手势事件)

上篇回顾:ArkTS开发系列之事件(2.8.1触屏、键鼠、焦点事件)

本篇内容:ArkTS开发系列之事件(2.8.2手势事件)

一、绑定手势方法

1. 常规手势绑定方法

复制代码
 Text('手势').fontSize(44)
        .gesture(TapGesture().onAction((event) => {
          console.error('event: ' + JSON.stringify(event))
        }))

2. 带优先级的手势绑定方法

  • 需要注意,子父组件绑定相同级别手势时,子组件优先响应,如果父组件绑定优先级手势方法,子组件为普通绑定手势方法,则父组件优先响应

    复制代码
      .priorityGesture(TapGesture().onAction((event)=>{
        console.error('parentGesture: ' + JSON.stringify(event))
      }))

3. 并行手势绑定方法

  • 当父组件绑定此手势方法时,父子组件可同时响应手势

    复制代码
      .parallelGesture(TapGesture().onAction((event)=>{
        console.error('parent event: ' + JSON.stringify(event))
      }))

二、单一手势

1. 点击手势(tapGesture)

复制代码
      Text('手势').fontSize(44)
        .gesture(TapGesture().onAction((event) => {
          console.error('event: ' + JSON.stringify(event))
        }))

2. 长按手势(longPressGesture)

复制代码
LongPressGesture(value?:{fingers?:number; repeat?:boolean; duration?:number})
  • fingers :触发最少手指数,默认1

  • repeat 是否连续触发 默认false

  • duration 长按多久触发,默认500

    Text('长按手势').fontSize(55)
    .gesture(
    LongPressGesture({fingers: 1, repeat: true, duration: 300})//fingers :触发最少手指数,默认1 repeat 是否连续触发 默认false duration 长按多久触发,默认500
    .onAction(event=>{
    console.error('longPress: ' + JSON.stringify(event))
    })
    )

3. 拖动手势(PanGesture)

复制代码
PanGesture(value?:{ fingers?:number; direction?:PanDirection; distance?:number})
  • fingers: 触发手势最少手指数,默认1

  • direction:触发手势方向,默认值Pandirection.All

  • distance:触发手势的最少距离,单位为vp,默认5vp

  • 有点类似于onTouch事件

    复制代码
    Text('拖动手势').fontSize(44)
          .gesture(
            PanGesture()
              .onActionStart(
                event => {
                  console.error('Pan start: ' + JSON.stringify(event))
                }
              )
              .onActionUpdate(
                event => {
                  console.error('Pan update: ' + JSON.stringify(event))
                }
              )
              .onActionEnd(
                event => {
                  console.error('Pan end: ' + JSON.stringify(event))
                }
              )
              .onActionCancel(
                () => {
                  console.error('Pan cancel: ')
                }
              )
          )

4. 撮合手势(PinchGesture)

复制代码
PinchGesture(value?:{fingers?:number; distance?:number})
  • fingers: 触发手势最少手指数,默认2, 最大值为5

  • distance:触发手势的最少距离,单位为vp,默认5vp

    复制代码
        Text('撮合手势').fontSize(44)
          .gesture(
            PinchGesture()
              .onActionStart(
                event => {
                  console.error('Pinch start: ' + JSON.stringify(event))
                }
              )
              .onActionUpdate(
                event => {
                  console.error('Pinch update: ' + JSON.stringify(event))
                }
              )
              .onActionEnd(
                event => {
                  console.error('Pinch end: ' + JSON.stringify(event))
                }
              )
              .onActionCancel(
                () => {
                  console.error('Pinch cancel: ')
                }
              )
          )

5. 旋转手势(RotationGesture)

复制代码
RotationGesture(value?:{fingers?:number; angle?:number})
  • fingers: 触发手势最少手指数,默认2, 最大值为5

  • angle: 触发手势的最小改变度数,单位是deg,默认为1deg

    Text().fontSize(44)
    .gesture(
    RotationGesture({fingers:2})
    .onActionStart(
    event => {
    console.error('Rotation start: ' + JSON.stringify(event))
    }
    )
    .onActionUpdate(
    event => {
    console.error('Rotation update: ' + JSON.stringify(event))
    }
    )
    .onActionEnd(
    event => {
    console.error('Rotation end: ' + JSON.stringify(event))
    }
    )
    .onActionCancel(
    () => {
    console.error('Rotation cancel: ')
    }
    )
    )

6. 滑动手势(SwipeGesture)

复制代码
SwipeGesture(value?:{fingers?:number; direction?:SwipeDirection; speed?:number})
  • fingers: 触发手势最少手指数,默认1, 最大值为10

  • direction: 触发手势的方向默认值是 SwipeDirection.All

  • speed: 触发手势的最小滑动速度,单位为vp/s,默认值为100vp/s

    复制代码
      .gesture(
        SwipeGesture({ direction: SwipeDirection.Vertical })
          .onAction(event => {
            console.error('Swipe : ' + JSON.stringify(event))
          })
      )

三、组合手势

复制代码
GestureGroup(mode:GestureMode, ...gesture:GestureType[])

mode: 声明组合手势的类型
gesture: 手势数组

1. 顺序组合

复制代码
    .gesture(
      GestureGroup(GestureMode.Sequence,
        SwipeGesture({ direction: SwipeDirection.Vertical })
          .onAction(event => {
            console.error('Swipe : ' + JSON.stringify(event))
          }),
        LongPressGesture()
          .onAction(event => {
            console.error('longPress : ' + JSON.stringify(event))
          })
      )
    )

2. 并行组合

复制代码
    .gesture(
      GestureGroup(GestureMode.Parallel,
        SwipeGesture({ direction: SwipeDirection.Vertical })
          .onAction(event => {
            console.error('Swipe : ' + JSON.stringify(event))
          }),
        LongPressGesture()
          .onAction(event => {
            console.error('longPress : ' + JSON.stringify(event))
          })
      )
    )

3. 互斥组合

复制代码
    .gesture(
      GestureGroup(GestureMode.Exclusive,
        SwipeGesture({ direction: SwipeDirection.Vertical })
          .onAction(event => {
            console.error('Swipe : ' + JSON.stringify(event))
          }),
        LongPressGesture()
          .onAction(event => {
            console.error('longPress : ' + JSON.stringify(event))
          })
      )
    )
相关推荐
excel1 天前
为什么相同卷积代码在不同层学到的特征完全不同——基于 tfjs-node 猫图像识别示例的逐层解析
前端
知识分享小能手1 天前
React学习教程,从入门到精通,React 使用属性(Props)创建组件语法知识点与案例详解(15)
前端·javascript·vue.js·学习·react.js·前端框架·vue
用户21411832636021 天前
dify案例分享-免费玩转即梦 4.0 多图生成!Dify 工作流从搭建到使用全攻略,附案例效果
前端
CodeSheep1 天前
稚晖君又开始摇人了,有点猛啊!
前端·后端·程序员
JarvanMo1 天前
Flutter Web vs Mobile:主要区别以及如何调整你的UI
前端
IT_陈寒1 天前
Java性能优化:从这8个关键指标开始,让你的应用提速50%
前端·人工智能·后端
天生我材必有用_吴用1 天前
Vue3+Node.js 实现大文件上传:断点续传、秒传、分片上传完整教程(含源码)
前端
摸鱼的春哥1 天前
前端程序员最讨厌的10件事
前端·javascript·后端
牧羊狼的狼1 天前
React 中的 HOC 和 Hooks
前端·javascript·react.js·hooks·高阶组件·hoc
知识分享小能手1 天前
React学习教程,从入门到精通, React 属性(Props)语法知识点与案例详解(14)
前端·javascript·vue.js·学习·react.js·vue·react