想要实现预期效果,可分为两步:
if (this.ifNeed) { // $r('app.media.startIcon')需要替换为开发者所需的图片资源文件 Image($r('app.media.startIcon')) .width(100) .height(100) .draggable(true) .visibility(this.imgState) .onDragStart(() => { this.imgState = Visibility.Hidden; }) .onDragEnd(() => { this.imgState = Visibility.Visible; }); }
Column() .margin({ left: 15 }) .backgroundColor('#F1F3F5') .borderRadius(8) // 图标拖入后将boolean值修改,不再展示图标 .onDrop(() => { this.ifNeed = false; })
@Entry @Component struct DropExample { @State ifNeed: boolean = true; @State imgState: Visibility = Visibility.Visible; build() { Row() { Column() { if (this.ifNeed) { // $r('app.media.startIcon')需要替换为开发者所需的图片资源文件 Image($r('app.media.startIcon')) .width(100) .height(100) .draggable(true) .visibility(this.imgState) .onDragStart(() => { this.imgState = Visibility.Hidden; }) .onDragEnd(() => { this.imgState = Visibility.Visible; }); } }.width('45%') .height('100%'); Column() { Text('垃圾桶区域') .fontSize(20) .width(180) .height(40) .textAlign(TextAlign.Center) .margin(10); Column() .margin({ left: 15 }) .backgroundColor('#F1F3F5') .borderRadius(8) // 图标拖入后将boolean值修改,不再展示图标 .onDrop(() => { this.ifNeed = false; }) .width(150) .height(150); }.width('45%') .height('100%') .margin({ left: '5%' }); } .height('100%'); } }