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