在Flex中,如何检测组件何时移动? [英] In Flex, how can I detect when a component moves?
问题描述
Application.application.addEventListener
(MoveEvent.MOVE,handleMove,true,0,true);
第三个参数是必需的,因为移动事件不会冒泡,所以必须被捕获。第四个参数是不重要的,第五个参数打开弱引用,在这种情况下,这是一个好主意,因为我们正在创建一个从Application.application到handleMove的全局引用 - 一个内存泄漏的处方。
当然,这会太频繁地发生(每当应用程序中的任何事情发生时都会发生一次),并且在大型应用程序中可能会导致性能问题。如果您知道在层次结构中有更高级别的组件可以保持不变,那么您可以将侦听器放在那个位置,而不是全局的,这样可以减少问题。
<不过,有一个更清晰的方法来解决这个问题将是很好的。
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);
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屋!