Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you change a drag-and-dropped image mid-drag?

So I want to drag an object from one source to multiple potential destinations. When the object is over each destination, I want it to 'morph' into a different image. Is there a straightforward way to do this from the NSDragSource perspective?

like image 914
Ben Gottlieb Avatar asked Oct 10 '09 03:10

Ben Gottlieb


People also ask

How do I drag and drop an image in HTML?

The image link is inserted in the HTML page using <img> src attribute. Whenever we are going to insert the image, we have to enable draggable=”true”. Also, enable ondragstart=”drag(event)” so that this image can be draggable along with setting the image width and height.

How do I drag and drop with mouse?

To move an object, place the mouse cursor over it, press and hold down the left mouse button, then move the mouse while still holding down the left mouse button. When you have "dragged" the object to the location you want, let go of the mouse button.


2 Answers

You can do so by using enumerateDraggingItemsWithOptions: method of NSDraggingInfo in your dragging destination's handler methods (i.e. - your implementation of NSDraggingDestination protocol).

For example:

- (NSDragOperation)draggingUpdated:(id < NSDraggingInfo >)sender
{
    NSImage* newDragImage = <take it from wherever>;
    [sender enumerateDraggingItemsWithOptions:0
                                      forView:sender.draggingSource
                                      classes:[NSArray arrayWithObject:[NSPasteboardItem class]]
                                searchOptions:nil
                                   usingBlock:^(NSDraggingItem *draggingItem, NSInteger idx, BOOL *stop) {
                                       NSRect theFrame = draggingItem.draggingFrame;
                                       theFrame.size = newDragImage.size;
                                       [draggingItem setDraggingFrame:theFrame contents:newDragImage];
                                       *stop = NO;
                                   }];
}
like image 87
Jonan Gueorguiev Avatar answered Sep 28 '22 01:09

Jonan Gueorguiev


Joshua Nozzi has posted a great way to do this: http://joshua.nozzi.name/2009/10/jlndrageffectmanager/

like image 20
Ben Gottlieb Avatar answered Sep 28 '22 00:09

Ben Gottlieb