1 Star 0 Fork 5.2K

OpenHarmony-build / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ts-gesture-settings.md 18.05 KB
一键复制 编辑 原始数据 按行查看 历史
zengyawen 提交于 2021-11-10 20:02 . add arkui

绑定手势方法

可以通过如下属性给组件绑定手势识别,手势识别成功后可以通过事件回调通知组件。

名称

参数类型

默认值

描述

gesture

gesture: GestureType,

mask?: GestureMask

gesture: -,

mask: GestureMask.Normal

绑定手势识别。

gesture: 绑定的手势类型, mask: 事件响应设置。

priorityGesture

gesture: GestureType,

mask?: GestureMask

gesture: -,

mask: GestureMask.Normal

绑定优先识别手势。

gesture: 绑定的手势类型, mask: 事件响应设置。

说明:
  • 默认情况下,子组件优先于父组件识别手势,当父组件配置priorityGesture时,父组件优先于子组件进行识别。

parallelGesture

gesture: GestureType,

mask?: GestureMask

gesture: -,

mask: GestureMask.Normal

绑定可与子组件手势同时触发的手势。

gesture: 绑定的手势类型, mask: 事件响应设置。

说明:
  • 手势事件为非冒泡事件。父组件设置parallelGesture时,父子组件相同的手势事件都可以触发,实现类似冒泡效果。
  • GestureMask枚举说明

    名称

    描述

    Normal

    不屏蔽子组件的手势,按照默认手势识别顺序进行识别。

    IgnoreInternal

    屏蔽子组件的手势,仅当前容器的手势进行识别。

    说明:
    • 子组件上系统内置的手势不会被屏蔽,如子组件为List组件时,内置的滑动手势仍然会触发。
  • 系统提供如下Gesture类型

    名称

    描述

    TapGesture

    点击手势,支持单次点击、多次点击识别。

    LongPressGesture

    长按手势。

    PanGesture

    平移手势。

    PinchGesture

    捏合手势。

    RotationGesture

    旋转手势。

    GestureGroup

    手势识别组,多种手势组合为复合手势,支持连续识别、并行识别和互斥识别。

响应手势事件

组件通过gesture方法绑定手势对象,可以通过手势对象提供的事件相应响应手势操作。如通过TapGesture对象的onAction事件响应点击事件。具体事件定义见各个手势对象章节。

  • TapGesture事件说明

    名称

    功能描述

    onAction((event?: GestureEvent) => void)

    Tap手势识别成功回调。

  • GestureEvent对象说明

    属性名称

    属性类型

    描述

    repeat

    boolean

    事件是否为重复触发事件,用于LongPressGesture场景。

    offsetX

    number

    手势事件偏移量,用于PanGesture手势触发场景,单位为vp。

    offsetY

    number

    手势事件偏移量,用于PanGesture手势触发场景,单位为vp。

    scale

    number

    缩放比例,用于PinchGesture手势触发场景。

    pinchCenterX

    number

    捏合手势中心点X轴坐标,用于PinchGesture手势触发场景,单位为px。

    pinchCenterY

    number

    捏合手势中心点Y轴坐标,用于PinchGesture手势触发场景,单位为px。

    angle

    number

    旋转角度,用于RotationGesture手势触发场景。

    timestamp

    number

    事件时间戳。

示例

@Entry
@Component
struct GestureSettingsExample {
  @State value: string = ''

  build() {
    Column() {
      Text('Click\n' + this.value).gesture(TapGesture()
        .onAction(() => {
          this.value = 'gesture onAction'
        }))
    }.height(200).width(300).padding(60).border({ width: 1 }).margin(30)
    //设置为priorityGesture时,会优先识别该绑定手势忽略内部gesture手势
    .priorityGesture(
      TapGesture()
        .onAction(() => {
          this.value = 'priorityGesture onAction'
        }), GestureMask.IgnoreInternal
    )
  }
}

1
https://gitee.com/openharmony-build/docs.git
git@gitee.com:openharmony-build/docs.git
openharmony-build
docs
docs
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891