1e41f4b71Sopenharmony_ci# LongPressGesture
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci用于触发长按手势事件,触发长按手势的最少手指数为1,最短长按时间为500毫秒。
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci>  **说明:**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci>  从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## 接口
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ciLongPressGesture(value?: { fingers?: number, repeat?: boolean, duration?: number })
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci当组件默认支持可拖拽时,如Text、TextInput、TextArea、HyperLink、Image和RichEditor等组件。长按手势与拖拽会出现冲突,事件优先级如下: 
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci长按触发时间 < 500ms,长按事件优先拖拽事件响应。 
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci长按触发时间 >= 500ms,拖拽事件优先长按事件响应。 
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci**参数:**
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci| 参数名称 | 参数类型 | 必填 | 参数描述 |
25e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
26e41f4b71Sopenharmony_ci| fingers | number | 否 | 触发长按的最少手指数,最小为1指,&nbsp;最大取值为10指。<br/>默认值:1 <br/> **说明:** <br/>手指按下后若发生超过15px的移动,则判定当前长按手势识别失败。|
27e41f4b71Sopenharmony_ci| repeat | boolean | 否 | 是否连续触发事件回调。<br/>默认值:false |
28e41f4b71Sopenharmony_ci| duration | number | 否 | 触发长按的最短时间,单位为毫秒(ms)。<br/>默认值:500 <br/>**说明:** <br/>设置小于等于0时,按照默认值500处理。|
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci## 事件
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci| 名称 | 功能描述 |
34e41f4b71Sopenharmony_ci| -------- | -------- |
35e41f4b71Sopenharmony_ci| onAction(event:(event:&nbsp;[GestureEvent](ts-gesture-settings.md#gestureevent对象说明))&nbsp;=&gt;&nbsp;void) | LongPress手势识别成功回调。 <br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
36e41f4b71Sopenharmony_ci| onActionEnd(event:(event:&nbsp;[GestureEvent](ts-gesture-settings.md#gestureevent对象说明))&nbsp;=&gt;&nbsp;void) | LongPress手势识别成功,最后一根手指抬起后触发回调。 <br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
37e41f4b71Sopenharmony_ci| onActionCancel(event:&nbsp;()&nbsp;=&gt;&nbsp;void) | LongPress手势识别成功,接收到触摸取消事件触发回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。|
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci## 属性
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci| 名称 | 类型    |描述                                        |
44e41f4b71Sopenharmony_ci| ----  | ------  | ---------------------------------------- |
45e41f4b71Sopenharmony_ci| tag<sup>11+</sup>   | string  | 设置LongPress手势标志,用于自定义手势判定时区分绑定的手势。|
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci## 示例
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci```ts
50e41f4b71Sopenharmony_ci// xxx.ets
51e41f4b71Sopenharmony_ci@Entry
52e41f4b71Sopenharmony_ci@Component
53e41f4b71Sopenharmony_cistruct LongPressGestureExample {
54e41f4b71Sopenharmony_ci  @State count: number = 0
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci  build() {
57e41f4b71Sopenharmony_ci    Column() {
58e41f4b71Sopenharmony_ci      Text('LongPress onAction:' + this.count).fontSize(28)
59e41f4b71Sopenharmony_ci        // 单指长按文本触发该手势事件
60e41f4b71Sopenharmony_ci        .gesture(
61e41f4b71Sopenharmony_ci        LongPressGesture({ repeat: true })
62e41f4b71Sopenharmony_ci          // 由于repeat设置为true,长按动作存在时会连续触发,触发间隔为duration(默认值500ms)
63e41f4b71Sopenharmony_ci          .onAction((event: GestureEvent) => {
64e41f4b71Sopenharmony_ci            if (event && event.repeat) {
65e41f4b71Sopenharmony_ci              this.count++
66e41f4b71Sopenharmony_ci            }
67e41f4b71Sopenharmony_ci          })
68e41f4b71Sopenharmony_ci            // 长按动作一结束触发
69e41f4b71Sopenharmony_ci          .onActionEnd((event: GestureEvent) => {
70e41f4b71Sopenharmony_ci            this.count = 0
71e41f4b71Sopenharmony_ci          })
72e41f4b71Sopenharmony_ci        )
73e41f4b71Sopenharmony_ci    }
74e41f4b71Sopenharmony_ci    .height(200)
75e41f4b71Sopenharmony_ci    .width(300)
76e41f4b71Sopenharmony_ci    .padding(20)
77e41f4b71Sopenharmony_ci    .border({ width: 3 })
78e41f4b71Sopenharmony_ci    .margin(30)
79e41f4b71Sopenharmony_ci  }
80e41f4b71Sopenharmony_ci}
81e41f4b71Sopenharmony_ci```
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci![zh-cn_image_0000001174264380](figures/zh-cn_image_0000001174264380.gif)
84