HarmonyOS 拖拽时动画会丢失

拖拽的时候动画会丢失该如何处理呢?

一开始是抖动状态,长按一个和另一个交换后,动画会导致消息,以及拖拽元素的动画也消失了,是否有能够保持交换位置后动画还是保持的方法?以及长按拖拽元素保持动画的方法?

import { uniformTypeDescriptor } from '@kit.ArkData';

@Entry
@Component
struct DraggableTextGrid {
  @State texts: string[] = ['1', '2', '3', '4', '5', '1', '2', '3', '4', '5'];
  @State draggedText: string = "";
  @State draggedIndex: number = -1;
  @State myRotate: number = 1.0;
  @State shouldAnimate: boolean = true;
  private animationRunning: boolean = false;

  aboutToAppear() {
    this.uiContext = this.getUIContext?.();
  }

  uiContext: UIContext | undefined = undefined;

  onPageShow(): void {
    this.initAnimation();
  }

  initAnimation() {
    if (!this.animationRunning && this.uiContext) {
      this.animationRunning = true;  // 标记动画为运行中
      this.uiContext.keyframeAnimateTo({ iterations: -1 }, [
        {
          duration: 100,
          event: () => {
            this.myRotate = -10;
          }
        },
        {
          duration: 100,
          event: () => {
            this.myRotate = 10;
          }
        }
      ]);
    }
  }

  build() {
    Grid() {
      ForEach(this.texts, (text: string, index) => {
        GridItem() {
          if (this.draggedIndex === index) {
            Text(text)
              .width('100%')
              .height('100%')
              .opacity(0)
          } else {
            Text(text)
              .width('100%')
              .height('100%')
              .draggable(true)
              .onDragStart(() => {
                this.draggedText = text;
                this.draggedIndex = index;
              })
              .onDragEnd(() => {
                this.draggedText = "";
                this.draggedIndex = -1;
              })
              .allowDrop([uniformTypeDescriptor.UniformDataType.TEXT])
              .onDrop((event: DragEvent) => {
                if (this.draggedIndex !== index) {
                  // 用于交换位置,同时保证动画不间断
                  const tempTexts = [...this.texts];
                  tempTexts.splice(this.draggedIndex, 1);
                  tempTexts.splice(index, 0, this.draggedText);
                  this.texts = tempTexts;  // 一次性更新文本数组,减少动画中断
                }
                this.draggedText = "";
                this.draggedIndex = -1;
              })
              .onDragMove((event) => {
                if (this.draggedIndex !== index) {
                  // 使用临时数组进行操作,避免直接操作状态导致重绘
                  const tempTexts = [...this.texts];
                  tempTexts.splice(this.draggedIndex, 1);
                  tempTexts.splice(index, 0, this.draggedText);
                  this.texts = tempTexts;  // 一次性更新
                }
                this.draggedIndex = index;
              })
              .border({ width: 1, color: Color.Black });
          }
        }
        .width('100%')
        .height(50)
        .margin({ bottom: 5, right: 5 })
        .rotate({
          z: 1,
          angle: this.myRotate
        });
      })
    }
    .columnsTemplate('1fr 1fr 1fr 1fr 1fr')
    .columnsGap(10)
    .rowsGap(10)
    .friction(0.6)
    .enableScrollInteraction(true)
    .multiSelectable(false)
    .edgeEffect(EdgeEffect.Spring)
    .scrollBar(BarState.On)
    .scrollBarColor(Color.Grey)
    .scrollBarWidth(4)
    .width('100%')
    .backgroundColor(0xFAEEE0)
    .height(300);
  }
}
HarmonyOS
5天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

可以使用循环定时器设置角度:

import { uniformTypeDescriptor } from '@kit.ArkData';

@Entry
@Component
struct DraggableTextGrid1 {
  @State texts: string[] = ['1', '2', '3', '4', '5', '1', '2', '3', '4', '5'];
  @State draggedText: string = "";
  @State draggedIndex: number = -1;
  @State myRotate: number = -10;
  @State shouldAnimate: boolean = true;

  aboutToAppear() {
    this.uiContext = this.getUIContext?.();
  }

  uiContext: UIContext | undefined = undefined;

  onPageShow(): void {
    this.startRangle();
  }

  startRangle(){
    setInterval(()=>{
      this.myRotate = this.myRotate === -10 ? 10 : -10;
    }, 200)
  }

  build() {
    Column() {
      Text('我的样例')
      Grid() {
        ForEach(this.texts, (text: string, index) => {
          GridItem() {
            if (this.draggedIndex === index) {
              Text(text)
                .width('100%')
                .height('100%')
                .opacity(0)
            } else {
              Text(text)
                .width('100%')
                .height('100%')
                .draggable(true)
                .onDragStart(() => {
                  this.draggedText = text;
                  this.draggedIndex = index;
                  console.info('onDragStart');
                })
                .onDragEnd(() => {
                  this.draggedText = "";
                  this.draggedIndex = -1;
                  console.info('onDragEnd');
                })
                .allowDrop([uniformTypeDescriptor.UniformDataType.TEXT])
                .onDrop((event: DragEvent) => {
                  if (this.draggedIndex !== index) {
                    // 用于交换位置,同时保证动画不间断
                    const tempTexts = [...this.texts];
                    tempTexts.splice(this.draggedIndex, 1);
                    tempTexts.splice(index, 0, this.draggedText);
                    this.texts = tempTexts; // 一次性更新文本数组,减少动画中断
                  }
                  this.draggedText = "";
                  this.draggedIndex = -1;
                  console.info('onDrop');
                })
                .onDragMove((event) => {
                  if (this.draggedIndex !== index) {
                    // 使用临时数组进行操作,避免直接操作状态导致重绘
                    const tempTexts = [...this.texts];
                    tempTexts.splice(this.draggedIndex, 1);
                    tempTexts.splice(index, 0, this.draggedText);
                    this.texts = tempTexts; // 一次性更新
                  }
                  this.draggedIndex = index;
                  console.info('onDragMove');
                })
                .border({ width: 1, color: Color.Black })
            }
          }
          .width('100%')
          .height(50)
          .margin({ bottom: 5, right: 5 })
          .rotate({
            z: 1,
            angle: this.myRotate
          })
          .backgroundColor(Color.Red)
        })
      }
      .columnsTemplate('1fr 1fr 1fr 1fr 1fr')
      .columnsGap(10)
      .rowsGap(10)
      .friction(0.6)
      .enableScrollInteraction(true)
      .multiSelectable(false)
      .edgeEffect(EdgeEffect.Spring)
      .scrollBar(BarState.On)
      .scrollBarColor(Color.Grey)
      .scrollBarWidth(4)
      .width('100%')
      .backgroundColor(0xFAEEE0)
      .height(300);
    }
  }
}
分享
微博
QQ
微信
回复
5天前
相关问题
HarmonyOS Grid拖拽动画问题
222浏览 • 1回复 待解决
HarmonyOS grid拖拽和增删动画效果
205浏览 • 1回复 待解决
HarmonyOS grid拖拽动画不流畅
178浏览 • 1回复 待解决
拖拽怎么设置当前拖拽项目数
1149浏览 • 1回复 待解决
HarmonyOS grid拖拽效果如何添加动画
194浏览 • 1回复 待解决
html sms标签跳转丢失&符号后内容
3066浏览 • 1回复 待解决
HarmonyOS emitterdata丢失问题
128浏览 • 1回复 待解决
HarmonyOS 数据精度丢失
348浏览 • 2回复 待解决
HarmonyOS 精度丢失问题
206浏览 • 2回复 待解决
HarmonyOS App启动动画怎么取消
198浏览 • 1回复 待解决
HarmonyOS 图片拖拽实现
263浏览 • 1回复 待解决
提问
该提问已有0人参与 ,帮助了0人