在flex4裡用spark建置可拖動panel

在flex4裡面的spark組件的panel是沒有內建拖動的功能的
因此若我們希望這物件要可以被拖動
我們必須要去自己實做當使用者拖動topGroup的區塊時的拖動動作

範例程式碼如下:
package components
{
import flash.events.MouseEvent;

import mx.managers.DragManager;

import spark.components.Group;
import spark.components.Panel;

/**
* A simple extension of the Spark Panel component
* that enables dragging.
*/
public class DraggablePanel extends Panel
{
//————————————–
// Constructor
//————————————–

public function DraggablePanel()
{
super();
}

//————————————–
// Skin Parts
//————————————–

/**
* The skin part that represents the title bar of the underlying Panel.
* NOTE: The default PanelSkin already has this, it’s just not defined as a skinPart in the Panel class.
* We want it so that we can capture dragging.
*/
[SkinPart(required=”true”)]
public var topGroup:Group;

//————————————–
// Overridden Methods
//————————————–

protected override function partAdded( partName:String, instance:Object ) : void
{
super.partAdded( partName, instance );

if (instance == topGroup)
{
Group( instance ).addEventListener( MouseEvent.MOUSE_DOWN, topGroup_mouseDownHandler );
Group( instance ).addEventListener( MouseEvent.MOUSE_UP, topGroup_mouseUpHandler );
}
}

protected override function partRemoved( partName:String, instance:Object ) : void
{
super.partRemoved( partName, instance );

if (instance == topGroup)
{
Group( instance ).removeEventListener( MouseEvent.MOUSE_DOWN, topGroup_mouseDownHandler );
Group( instance ).removeEventListener( MouseEvent.MOUSE_UP, topGroup_mouseUpHandler );
}
}

//————————————–
// Event Handlers
//————————————–

protected function topGroup_mouseDownHandler( event:MouseEvent ):void
{
if ( !DragManager.isDragging )
startDrag();
}

protected function topGroup_mouseUpHandler( event:MouseEvent ):void
{
stopDrag();
}
}


17年資歷女工程師,專精於動畫、影像辨識以及即時串流程式開發。經常組織活動,邀請優秀的女性分享她們的技術專長,並在眾多場合分享自己的技術知識,也活躍於非營利組織,辦理活動來支持特殊兒及其家庭。期待用技術改變世界。