• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.deviceStatus.dragInteraction (拖拽)
2
3 拖拽功能模块,提供注册和取消拖拽状态监听的能力。
4
5> **说明**
6>
7>   - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9>  - 本模块接口均为系统接口。
10
11## 导入模块
12
13```js
14import dragInteraction from '@ohos.deviceStatus.dragInteraction';
15```
16
17##  DragState
18
19拖拽状态。
20
21**系统能力**:SystemCapability.Msdp.DeviceStatus.Drag
22
23| 名称                       | 值                             | 说明                              |
24| --------                     |  -----------------               |  -----------------               |
25| MSG_DRAG_STATE_START |  1   | 表示开始拖拽。 |
26| MSG_DRAG_STATE_STOP |  2  |  表示结束拖拽。  |
27| MSG_DRAG_STATE_CANCEL |  3  |  表示取消拖拽。  |
28
29## dragInteraction.on('drag')
30
31on(type: 'drag', callback: Callback<DragState>): void;
32
33注册监听拖拽状态。
34
35**系统能力**:SystemCapability.Msdp.DeviceStatus.Drag
36
37**参数**:
38
39| 参数名                | 类型                                                             | 必填 | 说明                            |
40| --------             | ----------------------------                                    | ---- | ----------------------------   |
41| type                 | string                                                          |  是  | 监听类型,固定取值为 'drag' |
42| callback             | Callback<[DragState](#dragstate)> |  是  | 回调函数,异步返回拖拽状态消息 |
43
44**示例**:
45
46```ts
47enum DragState {
48    MSG_DRAG_STATE_START = 1,
49    MSG_DRAG_STATE_STOP = 2,
50    MSG_DRAG_STATE_CANCEL = 3
51}
52
53try {
54  dragInteraction.on('drag', (data : DragState) => {
55    console.log(`Drag interaction event: ${JSON.stringify(data)}`);
56  });
57} catch (error) {
58  console.log(`Register failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
59}
60```
61
62## dragInteraction.off('drag')
63
64off(type: 'drag', callback?: Callback<DragState>): void;
65
66取消监听拖拽状态。
67
68**系统能力**:SystemCapability.Msdp.DeviceStatus.Drag
69
70**参数**:
71
72| 参数名                | 类型                                                              | 必填    | 说明                           |
73| --------             | ----------------------------                                     | ----   | ----------------------------   |
74| type                 | string                                                           |  是    | 监听类型,固定取值为 'drag' |
75| callback             | Callback<[DragState](#dragstate)> |  否  | 需要取消注册的回调函数,若无此参数,则取消当前应用注册的所有回调函数。 |
76
77**示例**:
78
79```ts
80enum DragState {
81    MSG_DRAG_STATE_START = 1,
82    MSG_DRAG_STATE_STOP = 2,
83    MSG_DRAG_STATE_CANCEL = 3
84}
85
86// 取消注册单个回调函数
87function single_callback(event : DragState) {
88  console.log(`Drag interaction event: ${JSON.stringify(event)}`);
89  return false;
90}
91try {
92  dragInteraction.on('drag', single_callback);
93  dragInteraction.off("drag", single_callback);
94} catch (error) {
95  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
96}
97```
98```ts
99enum DragState {
100    MSG_DRAG_STATE_START = 1,
101    MSG_DRAG_STATE_STOP = 2,
102    MSG_DRAG_STATE_CANCEL = 3
103}
104
105// 取消注册所有回调函数
106function all_callback(event : DragState) {
107  console.log(`Drag interaction event: ${JSON.stringify(event)}`);
108  return false;
109}
110try {
111  dragInteraction.on('drag', all_callback);
112  dragInteraction.off("drag");
113} catch (error) {
114  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
115}
116```
117
118
119