HarmonyOS 元素使用了动画事件,会监听不到手势拖拽。去掉动画事件可以监听到手势拖拽。?

如题:HarmonyOS 元素使用了动画事件,会监听不到手势拖拽。去掉动画事件可以监听到手势拖拽。?

阅读 453
1 个回答

可以响应拖拽事件的示例,参考如下:

import { UIContext } from '@ohos.arkui.UIContext';

@Entry
@Component
struct KeyframeDemo {
  @State myScale: number = 1.0;
  uiContext: UIContext | undefined = undefined;
  aboutToAppear() {
    this.uiContext = this.getUIContext?.();
  }

  build() {
    Column() {
      Text('ceshi')
        .width(100)
        .height(100)
        .margin(100)
        .draggable(true)
        .scale({ x: this.myScale, y: this.myScale })
        .gesture(
          PanGesture({ direction: PanDirection.Left }).onActionStart(() => {
            console.log("success")
          })
        )
        .onClick(() => {
          if (!this.uiContext) {
            console.info("no uiContext, keyframe failed");
            return;
          }
          this.myScale = 1;
          // 设置关键帧动画整体播放3次
          this.uiContext.keyframeAnimateTo({ iterations: -1 }, [
            {
              // 第一段关键帧动画时长为800ms,scale属性做从1到1.5的动画
              duration: 800,
              event: () => {
                this.myScale = 1.5;
              }
            },
            {
              // 第二段关键帧动画时长为500ms,scale属性做从1.5到1的动画
              duration: 500,
              event: () => {
                this.myScale = 1;
              }
            }
          ]);
        })
    }.width('100%').margin({ top: 5 })
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进