ts-gesture-settings.md 4.2 KB
Newer Older
Z
zengyawen 已提交
1
# 绑定手势方法
Z
zengyawen 已提交
2

Z
zengyawen 已提交
3 4
> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
Z
zengyawen 已提交
5

Z
zengyawen 已提交
6 7

## 权限列表
Z
zengyawen 已提交
8 9 10



Z
zengyawen 已提交
11 12 13

## 绑定手势识别

Z
zengyawen 已提交
14 15 16

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

Z
zengyawen 已提交
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

| 名称 | 参数类型 | 默认值 | 描述 |
| -------- | -------- | -------- | -------- |
| gesture | gesture:&nbsp;GestureType,<br/>mask?:&nbsp;GestureMask | gesture:&nbsp;-,<br/>mask:&nbsp;GestureMask.Normal | 绑定手势识别。<br/>gesture:&nbsp;绑定的手势类型,&nbsp;mask:&nbsp;事件响应设置。 |
| priorityGesture | gesture:&nbsp;GestureType,<br/>mask?:&nbsp;GestureMask | gesture:&nbsp;-,<br/>mask:&nbsp;GestureMask.Normal | 绑定优先识别手势。<br/>gesture:&nbsp;绑定的手势类型,&nbsp;mask:&nbsp;事件响应设置。<br/>>&nbsp;![icon-note.gif](public_sys-resources/icon-note.gif)&nbsp;**说明:**<br/>>&nbsp;-&nbsp;默认情况下,子组件优先于父组件识别手势,当父组件配置priorityGesture时,父组件优先于子组件进行识别。 |
| parallelGesture | gesture:&nbsp;GestureType,<br/>mask?:&nbsp;GestureMask | gesture:&nbsp;-,<br/>mask:&nbsp;GestureMask.Normal | 绑定可与子组件手势同时触发的手势。<br/>gesture:&nbsp;绑定的手势类型,&nbsp;mask:&nbsp;事件响应设置。<br/>>&nbsp;![icon-note.gif](public_sys-resources/icon-note.gif)&nbsp;**说明:**<br/>>&nbsp;-&nbsp;手势事件为非冒泡事件。父组件设置parallelGesture时,父子组件相同的手势事件都可以触发,实现类似冒泡效果。 |


- GestureMask枚举说明
  | 名称 | 描述 | 
  | -------- | -------- |
  | Normal | 不屏蔽子组件的手势,按照默认手势识别顺序进行识别。 | 
  | IgnoreInternal | 屏蔽子组件的手势,仅当前容器的手势进行识别。<br/>>&nbsp;![icon-note.gif](public_sys-resources/icon-note.gif)&nbsp;**说明:**<br/>>&nbsp;子组件上系统内置的手势不会被屏蔽,如子组件为List组件时,内置的滑动手势仍然会触发。 | 


- GestureType
  | 名称 | 描述 | 
  | -------- | -------- |
  | TapGesture | 点击手势,支持单次点击、多次点击识别。 | 
  | LongPressGesture | 长按手势。 | 
  | PanGesture | 平移手势。 | 
  | PinchGesture | 捏合手势。 | 
  | RotationGesture | 旋转手势。 | 
  | GestureGroup | 手势识别组,多种手势组合为复合手势,支持连续识别、并行识别和互斥识别。 | 


## 响应手势事件
Z
zengyawen 已提交
44 45 46

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

Z
zengyawen 已提交
47 48
- TapGesture事件说明
  | 名称 | 功能描述 | 
49
  | -------- | -------- |
Z
zengyawen 已提交
50 51 52 53 54 55 56 57 58 59
  | onAction((event?:GestureEvent)&nbsp;=&gt;&nbsp;void) | Tap手势识别成功回调。 | 

- GestureEvent对象说明
  | 属性名称 | 属性类型 | 描述 |
  | -------- | -------- | -------- |
  | timestamp | number | 事件时间戳。 |
  | target<sup>8+</sup> | [EventTarget](ts-universal-events-click.md) | 触发手势事件的元素对象。 |


## 示例
Z
zengyawen 已提交
60

H
geshi  
HelloCrease 已提交
61 62
```ts
// xxx.ets
Z
zengyawen 已提交
63 64 65 66 67 68
@Entry
@Component
struct GestureSettingsExample {
  @State value: string = ''

  build() {
Z
zengyawen 已提交
69 70 71 72 73 74 75 76 77 78 79
    Column(){
      Column() {
        Text('Click\n' + this.value)
          .gesture(
          TapGesture()
            .onAction(() => {
              this.value = 'gesture onAction'
            }))
      }.height(200).width(300).padding(60).border({ width: 1 })
      //设置为priorityGesture时,会优先识别该绑定手势忽略内部gesture手势
      .priorityGesture(
Z
zengyawen 已提交
80
      TapGesture()
Z
zengyawen 已提交
81
        .onAction((event: GestureEvent) => {
Z
zengyawen 已提交
82
          this.value = 'priorityGesture onAction' + '\ncomponent globalPos:('
83
          + event.target.area.globalPosition.x + ',' + event.target.area.globalPosition.y + ')\nwidth:'
Z
zengyawen 已提交
84
          + event.target.area.width + '\nheight:' + event.target.area.height
Z
zengyawen 已提交
85
        }), GestureMask.IgnoreInternal
Z
zengyawen 已提交
86 87
      )
    }.padding(60)
Z
zengyawen 已提交
88 89 90 91
  }
}
```

Z
zengyawen 已提交
92
![zh-cn_image_0000001210195016](figures/zh-cn_image_0000001210195016.gif)