HarmonyOS 触摸手势相关的问题?

阅读 588
1 个回答

示例参考:

onWindowStageCreate(windowStage: window.WindowStage): void {
  // Main window is created, set main page for this ability
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
  windowStage.loadContent('pages/Navigation', (err) => {
  if (err.code) {
  hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
  return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
});
// 获取应用主窗口。
let windowClass: window.Window | undefined = undefined;
windowStage.getMainWindow((err: BusinessError, data) => {
  let errCode: number = err.code;
  if (errCode) {
    console.error('Failed to obtain the main window. Cause: ' + JSON.stringify(err));
    return;
  }
  windowClass = data;
  console.info('Succeeded in obtaining the main window. Data: ' + JSON.stringify(data));
  // 获取UIContext实例。
  uiContext = windowClass.getUIContext();
  AppStorage.setOrCreate('UIContext', uiContext);

  uiContext = AppStorage.get('UIContext');
  if (uiContext) {
    // callback是开发者定义的监听回调函数
    let callback = (event: GestureEvent) => {
      console.info('pjn Succeeded in obtaining the GestureEvent Data: ' + JSON.stringify(event));
    };
    let observer: UIObserver = uiContext.getUIObserver();
    observer.on('didClick', callback);
  }
})
}

需要注意的是应用中如果存在窗口的创建导致的UIcontext变化,需要重新绑定onclick事件。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进