给组件添加移动事件监听

亲, 转载请附本文链接哦~

package poolandlane
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.core.UIComponent;

	/**
	 * 组件移动监听工具类
	 * @author Admin
	 * 
	 */
	public class DragElement
	{
		private var downPoint:Point;
		private var elem:UIComponent;
		private static var instance:DragElement;
		
		public function DragElement()
		{
			if(instance != null)
			{
				throw new Error("instance is not null");
			}
			instance = this;
		}
		
		public static function getInstance():DragElement
		{
			if(instance == null)
			{
				instance = new DragElement();
			}
			return instance;
		}
		
		public function addDragEvent(ui:UIComponent):void
		{
			elem = ui;
			ui.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
		}
		
		private function mouseDownHandler(event:MouseEvent):void
		{
			downPoint = new Point(event.localX, event.localY);
			elem.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			elem.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		
		protected function mouseMoveHandler(event:MouseEvent):void
		{
			elem.x += event.localX - downPoint.x;
			elem.y += event.localY - downPoint.y;
			event.stopPropagation();
		}
		
		private function mouseUpHandler(event:MouseEvent):void
		{
			elem.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			elem.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			event.stopPropagation();
		}
	}
}


使用方法:

 DragElement.getInstance().addDragEvent(想要移动的组件id);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值