先写一个用于拖拽的管理类。代码如下
package com.util
{
import flash.events.MouseEvent;
import mx.core.Container;
public class DragManager
{
public static function moveObject(obj:Container):void
{
obj.addEventListener(MouseEvent.MOUSE_DOWN, objMouseDown);
obj.addEventListener(MouseEvent.MOUSE_UP, objMouseUp);
}
private static function objMouseDown(e:MouseEvent):void
{
e.currentTarget.startDrag();
}
private static function objMouseUp(e:MouseEvent):void
{
e.currentTarget.stopDrag();
}
}
}
接着在Flex页面上创建一个要拖拽的控件,然后调用这个上面的类中的moveObject方法就可以实现拖拽效果,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="12"
initialize="init()">
<mx:Panel x="41" y="28" width="250" height="200"
layout="absolute" id="panel" title="拖拽">
</mx:Panel>
<mx:Script>
<![CDATA[
import com.util.DragManager;
private function init():void
{
DragManager.moveObject(this.panel);
}
]]>
</mx:Script>
</mx:Application>