HarmonyOS 子窗口是否可手势移动

HarmonyOS
2024-12-18 17:03:11
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

参考demo:

import window from '@ohos.window';

interface Position {
  x: number,
  y: number
}
@Entry
@Component
struct FloatContent {
  @State message: string = 'float window'
  private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.All });
  // 创建位置变量,并使用@Watch监听,变量发生变化调用moveWindow方法移动窗口
  @State @Watch("moveWindow") windowPosition: Position = { x: 0, y: 0 };
  floatWindow: window.Window
  // 通过悬浮窗名称“floatWindow”获取到创建的悬浮窗
  aboutToAppear() {
    this.floatWindow = window.findWindow("floatWindow")
  }
  // 将悬浮窗移动到指定位置
  moveWindow() {
    this.floatWindow.moveWindowTo(this.windowPosition.x, this.windowPosition.y);
  }

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
    .gesture(
      // 绑定PanGesture事件,监听拖拽动作
      PanGesture(this.panOption)
        .onActionStart((event: GestureEvent) => {
          console.info('Pan start');
        })
        .onActionUpdate((event: GestureEvent) => {
          // 发生拖拽时,获取到触摸点的位置,并将位置信息传递给windowPosition
          this.windowPosition.x += event.offsetX;
          this.windowPosition.y += event.offsetY;
        })
        .onActionEnd(() => {
          console.info('Pan end');
        })
    )
    .border({
      style: BorderStyle.Dotted
    })
    .backgroundColor(Color.Yellow)
  }
}
分享
微博
QQ
微信
回复
2024-12-18 19:23:30
相关问题
如何生成一个可以交互的移动窗口
1222浏览 • 1回复 待解决
HarmonyOS 移动GridItem的Grid组件
684浏览 • 1回复 待解决
HarmonyOS LongPressGesture手势移动问题
737浏览 • 1回复 待解决
窗口加载的页面是否可以带参数
541浏览 • 1回复 待解决
HarmonyOS 如何拖拽窗口
204浏览 • 1回复 待解决
HarmonyOS 窗口弹出popup问题
254浏览 • 1回复 待解决
HarmonyOS 窗口路由切换问题
359浏览 • 1回复 待解决
HarmonyOS 创建窗口后相关问题
148浏览 • 1回复 待解决
如何通过代码关闭窗口
772浏览 • 1回复 待解决
HarmonyOS 窗口跳转页面的返回问题
529浏览 • 1回复 待解决
如何设置窗口的背景颜色?
623浏览 • 1回复 待解决
HarmonyOS 窗口页面返回事件无效
226浏览 • 1回复 待解决
HarmonyOS window.findWindow获取窗口错误
675浏览 • 1回复 待解决