在 Flex 中,如何检测组件何时移动? [英] In Flex, how can I detect when a component moves?

查看:20
本文介绍了在 Flex 中,如何检测组件何时移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个 Flex 组件直接移动时,它的 x 和 y 属性会发生变化,然后会在该组件上分派move"事件.没关系.

When a Flex component moves directly, so that its x and y properties change, then a "move" event is dispatched on the component. That's all fine.

然而,一个组件也可能由于其父组件移动——或父组件移动,等等而移动.现在,当父组件移动时,它的子组件只是移动",而不会在子组件上分配任何属性更改或移动事件.尽管如此,孩子们仍在屏幕上移动.那么如何检测这种广义运动呢?

However, a component may also move as a result of its parent component moving — or its parent moving, and so on. Now when a parent component moves, its children just "move along" without any properties changing or move events being dispatched on the children. Still, the children are moving on the screen. So how can you detect this kind of generalized movement?

推荐答案

一种解决方法是捕获应用程序中的所有移动事件:

One workaround is to capture all move events in the application:

Application.application.addEventListener
  (MoveEvent.MOVE, handleMove, true, 0, true);

第三个参数是必需的,因为移动事件不会冒泡,因此必须被捕获.第四个参数不重要,第五个参数开启弱引用,在这种情况下这是一个好主意,因为我们正在创建一个从 Application.application 到 handleMove 的全局引用——内存泄漏的一个秘诀.

The third argument is required because move events do not bubble, and so instead have to be captured. The fourth argument is unimportant, and the fifth argument turns on weak references, which is a good idea in this case because we are creating a global reference from Application.application to handleMove — a recipe for memory leaks.

当然,这会过于频繁地触发(每次应用程序中的任何内容移动时都会触发一次),并且在大型应用程序中可能会导致性能问题.如果您知道层次结构中较高的某个组件肯定会保持静止,则可以将侦听器置于该点而不是全局位置,这样可以减少问题.

Of course, this will fire too often (once each time anything whatsoever in the application moves), and in a large application could lead to performance problems. If you know that there is some component higher up in the hierarchy that’s sure to stay still, you can put the listener at that point instead of globally, which could reduce the problem.

不过,如果有更简洁的方法来解决这个问题会很好.

Still, it would be nice to have a cleaner way to solve this.

这篇关于在 Flex 中,如何检测组件何时移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆