Flex4中的拖动技术
下面列一个最简单的例子,在Flex中,拖动原来如此简单
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Script> <![CDATA[ private function startMove(event:Event):void{ Sprite(event.target).startDrag(); } private function stopMove(event:Event):void{ Sprite(event.target).stopDrag(); } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <mx:Button id="btn" mouseDown="startMove(event)" mouseUp="stopMove(event)" label="move"/> </s:Application>
如果需要拖动的时候多个控件可以一起拖动,可以先用BorderContainer组件先把控件包含进来
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <s:layout> <s:BasicLayout/> </s:layout> <fx:Script> <![CDATA[ private function startMove(event:Event):void{ Sprite(bc).startDrag(); } private function stopMove(event:Event):void{ Sprite(bc).stopDrag(); } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <s:BorderContainer id="bc" x="80" y="92" width="250" height="200" mouseDown="startMove(event)" mouseUp="stopMove(event)" borderVisible="true"> <s:Button x="62" y="62" label="按钮"/> <s:Button x="62" y="124" label="按钮"/> </s:BorderContainer> </s:Application>