js-apis-devicestatus-draginteraction (1).md 3.5 KB
Newer Older
S
shawn_he 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
# @ohos.deviceStatus.dragInteraction (Drag Interaction)

 The **dragInteraction** module provides the APIs to enable and disable listening for dragging status changes.

> **NOTE**
>
>   - The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
>
>  - The APIs provided by this module are system APIs.

## Modules to Import

```js
import dragInteraction from '@ohos.deviceStatus.dragInteraction'
```

## dragInteraction.on('drag')

on(type: 'drag', callback: Callback<DragState>): void;

Enables listening for dragging status changes.

**System capability**: SystemCapability.Msdp.DeviceStatus.Drag

**Parameters**

| Name               | Type                                                            | Mandatory| Description                           |
| --------             | ----------------------------                                    | ---- | ----------------------------   |
| type                 | string                                                          |  Yes | Event type. This field has a fixed value of **drag**.|
| callback             | Callback<[DragState](#dragstate)> |  Yes | Callback used to return the dragging status.|

**Example**

```js
try {
  dragInteraction.on('drag', (data) => {
    console.log(`Drag interaction event: ${JSON.stringify(data)}`);
  });
} catch (error) {
  console.log(`Register failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

## dragInteraction.off('drag')

off(type: 'drag', callback?: Callback<DragState>): void;

Disables listening for dragging status changes.

**System capability**: SystemCapability.Msdp.DeviceStatus.Drag

**Parameters**

| Name               | Type                                                             | Mandatory   | Description                          |
| --------             | ----------------------------                                     | ----   | ----------------------------   |
| type                 | string                                                           |  Yes   | Event type. This field has a fixed value of **drag**.|
| callback             | Callback<[DragState](#dragstate)> |  No | Callback to be unregistered. If this parameter is not specified, all callbacks registered by the current application will be unregistered.|

**Example**

```js
// Unregister a single callback.
function callback(event) {
  console.log(`Drag interaction event: ${JSON.stringify(event)}`);
  return false;
}
try {
  dragInteraction.on('drag', callback);
  dragInteraction.off("drag", callback);
} catch (error) {
  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```
```js
// Unregister all callbacks.
function callback(event) {
  console.log(`Drag interaction event: ${JSON.stringify(event)}`);
  return false;
}
try {
  dragInteraction.on('drag', callback);
  dragInteraction.off("drag");
} catch (error) {
  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
}
```

##  DragState

Enumerates dragging states.

**System capability**: SystemCapability.Msdp.DeviceStatus.Drag

| Name                      | Value                            | Description                             |
| --------                     |  -----------------               |  -----------------               |
| MSG_DRAG_STATE_START |  1   | Dragging starts.|
| MSG_DRAG_STATE_STOP |  2  |  Dragging is ended. |
| MSG_DRAG_STATE_CANCEL |  3  |  Dragging is canceled. |