• 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## Summary<sup>11+</sup>
30
31拖拽对象的数据摘要。
32
33**系统能力:** SystemCapability.Msdp.DeviceStatus.Drag
34
35| 名称       | 类型     | 必填 | 说明               |
36| ---------- | -------- | ---- | ------------------ |
37| dataType   | string   | 是   | 拖拽对象类型。     |
38| dataSize   | number   | 是   | 拖拽对象数据长度。 |
39
40## dragInteraction.on('drag')
41
42on(type: 'drag', callback: Callback\<[DragState](#dragstate)>): void;
43
44注册监听拖拽状态。
45
46**系统能力:** SystemCapability.Msdp.DeviceStatus.Drag
47
48**参数:**
49
50| 参数名   | 类型                               | 必填 | 说明                             |
51| -------- | ---------------------------------- | ---- | -------------------------------- |
52| type     | string                             | 是   | 监听类型,固定取值为 'drag'。    |
53| callback | Callback\<[DragState](#dragstate)> | 是   | 回调函数,异步返回拖拽状态消息。 |
54
55**示例:**
56
57```ts
58try {
59  dragInteraction.on('drag', (data: dragInteraction.DragState) => {
60    console.log(`Drag interaction event: ${JSON.stringify(data)}`);
61  });
62} catch (error) {
63  console.log(`Register failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
64}
65```
66
67## dragInteraction.off('drag')
68
69off(type: 'drag', callback?: Callback\<[DragState](#dragstate)>): void;
70
71取消监听拖拽状态。
72
73**系统能力:** SystemCapability.Msdp.DeviceStatus.Drag
74
75**参数:**
76
77| 参数名   | 类型                               | 必填 | 说明                                                                   |
78| -------- | ---------------------------------- | ---- | ---------------------------------------------------------------------- |
79| type     | string                             | 是   | 监听类型,固定取值为 'drag'。                                          |
80| callback | Callback\<[DragState](#dragstate)> | 否   | 需要取消注册的回调函数,若无此参数,则取消当前应用注册的所有回调函数。 |
81
82**示例:**
83
84```ts
85// 取消注册单个回调函数
86function single_callback(event: dragInteraction.DragState) {
87  console.log(`Drag interaction event: ${JSON.stringify(event)}`);
88  return false;
89}
90try {
91  dragInteraction.on('drag', single_callback);
92  dragInteraction.off("drag", single_callback);
93} catch (error) {
94  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
95}
96```
97
98```ts
99// 取消注册所有回调函数
100function all_callback(event: dragInteraction.DragState) {
101  console.log(`Drag interaction event: ${JSON.stringify(event)}`);
102  return false;
103}
104try {
105  dragInteraction.on('drag', all_callback);
106  dragInteraction.off("drag");
107} catch (error) {
108  console.log(`Execute failed, error: ${JSON.stringify(error, [`code`, `message`])}`);
109}
110```
111
112## dragInteraction.getDataSummary()<sup>11+</sup>
113
114getDataSummary(): Array\<[Summary](#summary11)>;
115
116获取所有拖拽对象的摘要。
117
118**系统能力:** SystemCapability.Msdp.DeviceStatus.Drag
119
120**返回值:**
121
122| 类型                          | 说明                                                 |
123| ----------------------------- | ---------------------------------------------------- |
124| Array\<[Summary](#summary11)> | 所有拖拽对象的数据摘要,包含拖拽对象的类型和数据长度。 |
125
126**示例:**
127
128```ts
129let summarys: Array<dragInteraction.Summary> = dragInteraction.getDataSummary();
130console.log(`Drag interaction summarys: ${JSON.stringify(summarys)}`);
131```