harmony 鸿蒙Drag Event

2022-08-09 浏览 (974)

Drag Event

A drag event is triggered when a component is dragged.

NOTE

The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.

The resource files preset in the application (that is, the resource files that are contained in the HAP file before the application is installed) can be dragged and dropped only within the application.

The ArkUI framework implements the drag and drop capability for the following components, allowing them to serve as the drag source (from which data can be dragged) or drop target (to which data can be dropped). To enable drag and drop for these components, you only need to set their draggable attribute to true.

  • The following component supports drag actions by default: <Search>, <TextInput>, <TextArea>, <RichEditor>, <Text>, <Image>, <FormComponent>, <Hyperlink>

  • The following component supports drop actions by default: <Search>, <TextInput>, <TextArea>, <Video>

You can also define drag responses by implementing common drag events.

To enable drag and drop for other components, you need to set the draggable attribute to true and implement data transmission in APIs such as onDragStart.

Events

NameBubbling SupportedDescription
onDragStart(event: (event?: DragEvent, extraParams?: string) => CustomBuilder |DragItemInfo)NoTriggered when the component bound to the event is dragged for the first time.
- event: information about the drag event. For details, see DragEvent.
- extraParams: additional information about the drag event. For details, see extraParams.
Return value: component information displayed during dragging.
Trigger condition: long press for at least 500 ms.
Event priority:
- Long press time < 500 ms: Long press event > Drag event
- Other: Drag event > Long press event
onDragEnter(event: (event?: DragEvent, extraParams?: string) => void)NoTriggered when the dragged item enters a valid drop target.
- event: information about the drag event, including the coordinates of the item that is being dragged.
- extraParams: additional information about the drag event. For details, see extraParams.
This event is valid only when a listener for the onDrop event is enabled.
onDragMove(event: (event?: DragEvent, extraParams?: string) => void)NoTriggered when the dragged item moves in a valid drop target.
- event: information about the drag event, including the coordinates of the item that is being dragged.
- extraParams: additional information about the drag event. For details, see extraParams.
This event is valid only when a listener for the onDrop event is enabled.
onDragLeave(event: (event?: DragEvent, extraParams?: string) => void)NoTriggered when the dragged item leaves a valid drop target.
- event: information about the drag event, including the coordinates of the item that is being dragged.
- extraParams: additional information about the drag event. For details, see extraParams.
This event is valid only when a listener for the onDrop event is enabled.
onDrop(event: (event?: DragEvent, extraParams?: string) => void)NoTriggered when the dragged item is dropped on a valid drop target.
- event: information about the drag event, including the coordinates of the item that is being dragged.
- extraParams: additional information about the drag event. For details, see extraParams.
onDragEnd(event: (event?: DragEvent, extraParams?: string) => void)10+NoTriggered when the dragging of the component bound to the event ends.
- event: information about the drag event, including the coordinates of the item that is being dragged.
- extraParams: additional information about the drag event. For details, see extraParams.

DragItemInfo

NameTypeMandatoryDescription
pixelMapPixelMapNoImage to be displayed during dragging.
builderCustomBuilderNoCustom component to display during dragging. If pixelMap is set, this attribute is ignored.
extraInfostringNoExtra information of the dragged item.

extraParams

Provides additional information required for dragging an item.

extraParams is a string converted from a JSON object. You can obtain the following attributes using the JSON object converted from Json.parse.

NameTypeDescription
selectedIndexnumberIndex of the dragged item in the parent container. The value of selectedindex starts from 0.
This parameter is valid only in the drag event of the <ListItem> component.
insertIndexnumberIndex of the element into which the dragged item is dropped in the List component. The value of insertIndex starts from 0.
This parameter is valid only in the drag event of the <List> component.

DragEvent

NameTypeDescription
useCustomDropAnimation10+booleanWhether to use the default drop animation when the dragging ends.
setData(unifiedData: UnifiedData)10+voidSets drag-related data in the drag event.
getData()10+UnifiedDataObtains drag-related data from the drag event. For details about the data obtaining result, see the error code description.
getSummary()10+SummaryObtains the summary of drag-related data from the drag event.
setResult(dragRect: DragResult)10+voidSets the drag and drop result in the drag event.
getResult()10+DragResultObtains the drag and drop result from the drag event.
getPreviewRect()10+RectangleObtains the rectangle where the preview image is located.
getVelocityX()10+numberObtains the dragging velocity along the x-axis. The origin of the coordinate axis is the upper left corner of the screen. The unit is vp. The velocity is positive if the movement is from left to right, and it is negative if the movement is from right to left.
getVelocityY()10+numberObtains the dragging velocity along the y-axis. The origin of the coordinate axis is the upper left corner of the screen. The unit is vp. The velocity is positive if the movement is from top to bottom, and it is negative if the movement is from bottom to top.
getVelocity()10+numberObtains the dragging velocity along the main axis. The value is the arithmetic square root of the sum of squares of the velocity along the x- and y-axis.
getWindowX()10+numberX coordinate of the drag position relative to the upper left corner of the window, in vp.
getWindowY()10+numberY coordinate of the drag position relative to the upper left corner of the window, in vp.
getDisplayX()10+numberX coordinate of the drag position relative to the upper left corner of the screen, in vp.
getDisplayY()10+numberY coordinate of the drag position relative to the upper left corner of the screen, in vp.
getX()(deprecated)numberX coordinate of the drag position relative to the upper left corner of the window, in vp.
This API is deprecated since API version 10. You are advised to use getWindowX() instead.
getY()(deprecated)numberY coordinate of the drag position relative to the upper left corner of the window, in vp.
This API is deprecated since API version 10. You are advised to use getWindowY() instead.

Error codes

For details about the error codes, see Drag Event Error Codes.

IDError Message
190001GetData failed, data not found.
190002GetData failed, data error.

DragResult10+

NameDescription
DRAG_SUCCESSFULThe drag and drop operation succeeded.
DRAG_FAILEDThe drag and drop operation failed.
DRAG_CANCELEDThe drag and drop operation was canceled.
DROP_ENABLEDThe component allows for a drop operation.
DROP_DISABLEDThe component does not allow for a drop operation.

Example

import UDC from '@ohos.data.unifiedDataChannel';
import UTD from '@ohos.data.uniformTypeDescriptor';
import promptAction from '@ohos.promptAction';
import { BusinessError } from '@ohos.base';

@Entry
@Component
struct Index {
  @State targetImage: string = '';
  @State targetText: string = 'Drag Text';
  @State imageWidth: number = 100;
  @State imageHeight: number = 100;
  @State imgState: Visibility = Visibility.Visible;
  @State videoSrc: string = 'resource://RAWFILE/02.mp4';
  @State abstractContent: string = "abstract";
  @State textContent: string = "";

  getDataFromUdmfRetry(event: DragEvent, callback: (data: DragEvent)=>void)
  {
    try {
      let data:UnifiedData = event.getData();
      if (!data) {
        return false;
      }
      let records: Array<UDC.UnifiedRecord> = data.getRecords();
      if (!records||records.length <= 0) {
        return false;
      }
      callback(event);
      return true;
    } catch (e) {
      console.log("getData failed, code = " + (e as BusinessError).code + ", message = " + (e as BusinessError).message);
      return false;
    }
  }

  getDataFromUdmf(event: DragEvent, callback: (data: DragEvent)=>void)
  {
    if(this.getDataFromUdmfRetry(event, callback)) {
      return;
    }
    setTimeout(()=>{
      this.getDataFromUdmfRetry(event, callback);
    }, 1500);
  }

  build() {
    Row() {
      Column() {
        Text('start Drag')
          .fontSize(18)
          .width('100%')
          .height(40)
          .margin(10)
          .backgroundColor('#008888')
        Image($r('app.media.icon'))
          .width(100)
          .height(100)
          .draggable(true)
          .margin({left: 15})
          .visibility(this.imgState)
          .onDragEnd((event)=>{
            if (event.getResult() === DragResult.DRAG_SUCCESSFUL) {
              promptAction.showToast({duration: 100, message: 'Drag Success'});
            } else if (event.getResult() === DragResult.DRAG_FAILED) {
              promptAction.showToast({duration: 100, message: 'Drag failed'});
            }
          })
        Text('test drag event')
          .width('100%')
          .height(100)
          .draggable(true)
          .margin({left: 15})
          .copyOption(CopyOptions.InApp)
        TextArea({placeholder: 'please input words'})
          .copyOption(CopyOptions.InApp)
          .width('100%')
          .height(50)
          .draggable(true)
        Search({placeholder: 'please input you word'})
          .searchButton('Search')
          .width('100%')
          .height(80)
          .textFont({size: 20})
        Column() {
          Text('change video source')
        }.draggable(true)
        .onDragStart((event)=>{
          let video: UDC.Video = new UDC.Video();
          video.videoUri = '/resources/rawfile/01.mp4';
          let data: UDC.UnifiedData = new UDC.UnifiedData(video);
          (event as DragEvent).setData(data);
        })
        Column() {
          Text('this is abstract')
            .fontSize(20)
            .width('100%')
        }.margin({left: 40, top: 20})
        .width('100%')
        .height(100)
        .onDragStart((event)=>{
          let data: UDC.PlainText = new UDC.PlainText();
          data.abstract = 'this is abstract';
          data.textContent = 'this is content this is content';
          (event as DragEvent).setData(new UDC.UnifiedData(data));
        })
      }.width('45%')
      .height('100%')
      Column() {
        Text('Drag Target Area')
          .fontSize(20)
          .width('100%')
          .height(40)
          .margin(10)
          .backgroundColor('#008888')
        Image(this.targetImage)
          .width(this.imageWidth)
          .height(this.imageHeight)
          .draggable(true)
          .margin({left: 15})
          .border({color: Color.Black, width: 1})
          .allowDrop([UTD.UniformDataType.IMAGE])
          .onDrop((dragEvent?: DragEvent)=> {
            this.getDataFromUdmf((dragEvent as DragEvent), (event:DragEvent) => {
              let records: Array<UDC.UnifiedRecord> = event.getData().getRecords();
              let rect: Rectangle = event.getPreviewRect();
              this.imageWidth = Number(rect.width);
              this.imageHeight = Number(rect.height);
              this.targetImage = (records[0] as UDC.Image).imageUri;
              event.useCustomDropAnimation = false;
              animateTo({duration: 1000}, ()=>{
                this.imageWidth = 100;
                this.imageHeight = 100;
                this.imgState = Visibility.None;
              })
              event.setResult(DragResult.DRAG_SUCCESSFUL);
            })
          })

        Text(this.targetText)
          .width('100%')
          .height(100)
          .border({color: Color.Black, width: 1})
          .margin(15)
          .allowDrop([UTD.UniformDataType.TEXT])
          .onDrop((dragEvent?: DragEvent)=>{
            this.getDataFromUdmf((dragEvent as DragEvent), (event:DragEvent) => {
              let records:Array<UDC.UnifiedRecord> = event.getData().getRecords();
              let plainText:UDC.PlainText = records[0] as UDC.PlainText;
              this.targetText = plainText.textContent;
            })
          })

        Video({src: this.videoSrc, previewUri: $r('app.media.icon')})
          .width('100%')
          .height(200)
          .controls(true)
          .allowDrop([UTD.UniformDataType.VIDEO])

        Column() {
          Text(this.abstractContent).fontSize(20).width('100%')
          Text(this.textContent).fontSize(15).width('100%')
        }.width('100%').height(100).margin(20).border({color: Color.Black, width: 1})
        .allowDrop([UTD.UniformDataType.PLAIN_TEXT])
        .onDrop((dragEvent?: DragEvent)=>{
          this.getDataFromUdmf((dragEvent as DragEvent), (event:DragEvent) => {
            let records: Array<UDC.UnifiedRecord> = event.getData().getRecords();
            let plainText: UDC.PlainText = records[0] as UDC.PlainText;
            this.abstractContent = plainText.abstract as string;
            this.textContent = plainText.textContent;
          })
        })
      }.width('45%')
      .height('100%')
      .margin({left: '5%'})
    }
    .height('100%')
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkTS-based Declarative Development Paradigm

harmony 鸿蒙@ohos.multimedia.avCastPicker (AVCastPicker)

harmony 鸿蒙Property Animation

harmony 鸿蒙Enums

harmony 鸿蒙Blank

harmony 鸿蒙Button

harmony 鸿蒙CalendarPicker

harmony 鸿蒙Checkbox

harmony 鸿蒙CheckboxGroup

harmony 鸿蒙DataPanel

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/6ca9a8a60beb480491a2d5f597515447