...{ //判断是否有item被击中 protected function getItemUnderMouse ():Container ...{ var pt:Point = localToGlobal(_mouseDownPt); var i:int=0; for (i; i < _items.length; i++)...{ var item:Container = _items.getItemAt(i) as Container; if (item.hitTestPoint(pt.x, pt.y)) return item; } returnnull; } //在mouseMove事件中触发拖拉发起事件 protected function onMouseMove (evt:MouseEvent):void...{ if (!enabled ||!_dataProvider || _dataProvider.length <1) return; if (_isPressed)...{ if (dragEnabled &&!DragManager.isDragging && _mouseDownPt)...{ _draggedItem = getItemUnderMouse(); if (_draggedItem ==this|| _draggedItem ==null) return; var pt:Point =new Point(evt.localX, evt.localY); pt = DisplayObject(evt.target).localToGlobal(pt); _mouseDownPt = globalToLocal(pt); var dragEvt:DragEvent =new DragEvent(DragEvent.DRAG_START); dragEvt.draggedItem = _draggedItem; dragEvt.dragInitiator =this; dragEvt.localX = _mouseDownPt.x - _draggedItem.x; dragEvt.localY = _mouseDownPt.y - _draggedItem.y; dragEvt.buttonDown =true; dispatchEvent(dragEvt); } } } //当拖放属性被设置时,增加监听句柄 ...{ public function set dragEnabled (value:Boolean):void...{ if (_dragEnabled &&!value)...{ removeEventListener(DragEvent.DRAG_START, onDragStart); removeEventListener(DragEvent.DRAG_COMPLETE, onDragComplete); } _dragEnabled = value; if (value)...{ addEventListener(DragEvent.DRAG_START, onDragStart); addEventListener(DragEvent.DRAG_COMPLETE, onDragComplete); } } public function set dropEnabled (value:Boolean):void...{ if (_dropEnabled &&!value)...{ removeEventListener(DragEvent.DRAG_DROP, onDragDrop); removeEventListener(DragEvent.DRAG_ENTER, onDragEnter); removeEventListener(DragEvent.DRAG_EXIT, onDragExit); removeEventListener(DragEvent.DRAG_OVER, onDragOver); } _dropEnabled = value; if (value)...{ addEventListener(DragEvent.DRAG_DROP, onDragDrop); addEventListener(DragEvent.DRAG_ENTER, onDragEnter); addEventListener(DragEvent.DRAG_EXIT, onDragExit); addEventListener(DragEvent.DRAG_OVER, onDragOver); } } } //事件处理句柄 ...{ protected function onDragComplete (evt:DragEvent):void...{ //do nothing } protected function onDragDrop (evt:DragEvent):void...{ if (!dataProvider) dataProvider = []; if (evt.dragSource.hasFormat('items'))...{ _mouseDownPt =new Point(evt.localX, evt.localY); var i:int= dataProvider.length; if (evt.dragInitiator ==this&& dataProvider.length >0) i--; _mouseOverItem = getItemUnderMouse(); if (_mouseOverItem) i = _items.getItemIndex(_mouseOverItem); if (evt.action == DragManager.MOVE && evt.dragInitiator ==this)...{ _isManualViewUpdate =true; //update the dataProvider var oldIndex:int= _items.getItemIndex(_draggedItem); var data:Object = _dataProvider.removeItemAt(oldIndex); _dataProvider.addItemAt(data, i); //then update the view via a move-Change event var collectionEvt:CollectionEvent =new CollectionEvent(CollectionEvent.COLLECTION_CHANGE); collectionEvt.kind = CollectionEventKind.MOVE; collectionEvt.location = i; collectionEvt.oldLocation = oldIndex; collectionEvt.items =new Array(_draggedItem); collectionChange_moveLogic(collectionEvt); dispatchEvent(collectionEvt); return; } if (evt.action == DragManager.COPY && evt.dragInitiator ==this)...{ oldIndex = _items.getItemIndex(_draggedItem); var dataToCopy:Object = _dataProvider.getItemAt(oldIndex); } } } protected function onDragEnter (evt:DragEvent):void...{ if (evt.dragSource.hasFormat('items'))...{ DragManager.acceptDragDrop(this); DragManager.showFeedback(evt.ctrlKey ? DragManager.COPY : DragManager.MOVE); return; } DragManager.showFeedback(DragManager.NONE); } protected function onDragExit (evt:DragEvent):void...{ DragManager.showFeedback(DragManager.NONE); } protected function onDragOver (evt:DragEvent):void...{ if (evt.dragSource.hasFormat('items'))...{ DragManager.showFeedback(evt.ctrlKey ? DragManager.COPY : DragManager.MOVE); return; } DragManager.showFeedback(DragManager.NONE); } protected function onDragStart (evt:DragEvent):void...{ var data:Object = IListItemRenderer(evt.draggedItem).data; var dragSource:DragSource =new DragSource(); dragSource.addData(data, 'items'); var bitmap:Bitmap = SpriteUtil.render(IListItemRenderer(evt.draggedItem)); var dragProxy:Container =new Container(); dragProxy.width = evt.draggedItem.width; dragProxy.height = evt.draggedItem.height; dragProxy.rawChildren.addChild(bitmap); DragManager.doDrag(evt.dragInitiator, dragSource, evt, dragProxy); } } }
<?xml version="1.0"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> <mx:Script> <![CDATA[ import mx.core.DragSource; import mx.managers.DragManager; import mx.events.*; import mx.containers.Canvas; // Called when the user clicks the mouse on either colored canvas. // Initializes the drag. private function dragIt(event:MouseEvent, text:String, format:String):void { // Get the drag initiator component from the event object. var dragInitiator:Canvas=Canvas(event.currentTarget); // Create a DragSource object. var ds:DragSource = new DragSource(); // Add the data to the object. ds.addData(text, format); // Create a Canvas container to use as the drag proxy. // You must specify a size for the proxy image, // or else it will not appear. var canvasProxy:Canvas = new Canvas(); canvasProxy.width=30; canvasProxy.height=30; canvasProxy.setStyle('backgroundColor', dragInitiator.getStyle('backgroundColor')); // Call the DragManager doDrag() method to start the drag. // For information on this method, see // the "Initiating the drag" section. DragManager.doDrag(dragInitiator, ds, event, canvasProxy); } // Called if the user dragged a proxy onto the drop target canvas. private function doDragEnter(event:DragEvent):void { // Get the drop target component from the event object. var dropTarget:Canvas=Canvas(event.currentTarget); // Accept the drag only if the user is dragging data // identified by the 'color' format value. if (event.dragSource.hasFormat('color')) { DragManager.acceptDragDrop(dropTarget); } } // Called if the target accepts the dragged object and the user // releases the mouse button while over the canvas. // Handles the dragDrop event for the List control. private function doDragDrop(event:DragEvent):void { // Get the data identified by the color format from the drag source. var data:Object = event.dragSource.dataForFormat('color'); // Set the canvas color. myCanvas.setStyle("backgroundColor", data); } ]]> </mx:Script> <!-- A horizontal box with red and green canvases the user can drag --> <mx:HBox> <mx:Canvas backgroundColor="red" borderStyle="solid" width="30" height="30" mouseMove="dragIt(event, 'red', 'color');"/> <mx:Canvas backgroundColor="green" borderStyle="solid" width="30" height="30" mouseMove="dragIt(event, 'green', 'color');"/> </mx:HBox> <mx:Label text="Drag the item into this canvas"/> <!-- Handles dragEnter and dragDrop events to allow dropping --> <mx:Canvas id="myCanvas" backgroundColor="#FFFFFF" borderStyle="solid" width="100" height="100" dragEnter="doDragEnter(event);" dragDrop="doDragDrop(event);"/> </mx:Application>