ts-basic-gestures-longpressgesture.md 2.1 KB
Newer Older
Z
zengyawen 已提交
1
# LongPressGesture
Z
zengyawen 已提交
2

3 4 5 6 7
**LongPressGesture** is used to trigger a long press gesture, which requires one or more fingers with a minimum 500 ms hold-down time.

>  **NOTE**
>
>  This gesture is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8

Z
zengyawen 已提交
9 10 11

## APIs

E
ester.zhou 已提交
12
LongPressGesture(value?: { fingers?: number, repeat?: boolean, duration?: number })
Z
zengyawen 已提交
13

E
ester.zhou 已提交
14 15 16 17 18 19 20
**Parameters**

| Name| Type| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| fingers | number | No| Minimum number of fingers to trigger a long press gesture. The value ranges from 1 to 10.<br>Default value: **1**|
| repeat | boolean | No| Whether to continuously trigger the event callback.<br>Default value: **false**|
| duration | number | No| Minimum hold-down time, in ms.<br>Default value: **500**|
Z
zengyawen 已提交
21 22 23 24


## Events

25
| Name| Description|
Z
zengyawen 已提交
26
| -------- | -------- |
E
ester.zhou 已提交
27 28
| onAction(event:(event?: [GestureEvent](ts-gesture-settings.md)) =&gt; void) | Callback invoked when a long press gesture is recognized.|
| onActionEnd(event:(event?: [GestureEvent](ts-gesture-settings.md)) =&gt; void) | Callback invoked when the finger used for a long press gesture is lift.|
29
| onActionCancel(event: () =&gt; void) | Callback invoked when a tap cancellation event is received after a long press gesture is recognized.|
Z
zengyawen 已提交
30 31 32 33


## Example

34 35
```ts
// xxx.ets
Z
zengyawen 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
@Entry
@Component
struct LongPressGestureExample {
  @State count: number = 0

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
      Text('LongPress onAction:' + this.count)
    }
    .height(200).width(300).padding(60).border({ width:1 }).margin(30)
    .gesture(
      LongPressGesture({ repeat: true })
        // Repeatedly triggered when the long press gesture exists.
E
ester.zhou 已提交
49
        .onAction((event: GestureEvent) => {
Z
zengyawen 已提交
50 51 52 53 54 55 56 57 58 59 60
          if (event.repeat) { this.count++ }
        })
        // Triggered when the long press gesture ends.
        .onActionEnd(() => {
          this.count = 0
        })
    )
  }
}
```

Z
zengyawen 已提交
61
![en-us_image_0000001257058425](figures/en-us_image_0000001257058425.gif)