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

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

问题描述

当一个Flex组件直接移动,以便它的x和y属性发生变化时,组件上会发出一个move事件。没关系。然而,一个组件也可能由于其父组件的移动而移动,或者它的父组件移动,等等。现在当一个父组件移动时,它的子组件只是移动而没有任何属性改变,或者移动被分派给子组件的事件。不过,孩子们正在屏幕上移动。那么怎样才能检测到这种广义运动呢?解决方案一个解决方法是捕获应用程序中的所有移动事件:

p>

  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屋!

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