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

4

8

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?

Fusible answered 10/10, 2009 at 3:27 Comment(0)
A
3

There's no way to do this using just the drag source APIs.

If you look at Interface Builder it does something similar to what you want. When you drag a button out of the library the button animates in the middle of the drag.

According to most people the way Apple is doing this is by making the drag image a small transparent image. Then they create a Window and have it follow the mouse. You can then use the window to do any type of animation you want.

Antiphonal answered 10/10, 2009 at 5:58 Comment(1)
At least as of 10.7+ the accepted response is no longer correct - see Иван Георгиев answer for the current way to change stuff mid-drag!Basenji
B
19

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;
                                   }];
}
Bufford answered 13/6, 2013 at 8:53 Comment(0)
F
4

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

Fusible answered 27/11, 2009 at 22:5 Comment(3)
Yep. This is a drop-in manager for this particular effect. The download contains a demo app.Galloon
If you end up using this class, just let me know and I'll add your app to the list on the source page.Galloon
The link is dead.Devlen
A
3

There's no way to do this using just the drag source APIs.

If you look at Interface Builder it does something similar to what you want. When you drag a button out of the library the button animates in the middle of the drag.

According to most people the way Apple is doing this is by making the drag image a small transparent image. Then they create a Window and have it follow the mouse. You can then use the window to do any type of animation you want.

Antiphonal answered 10/10, 2009 at 5:58 Comment(1)
At least as of 10.7+ the accepted response is no longer correct - see Иван Георгиев answer for the current way to change stuff mid-drag!Basenji
E
3

Check out NSDraggingInfo's enumerateDraggingItemsWithOptions method.

Eisler answered 28/8, 2012 at 23:11 Comment(1)
Please edit your answer to elaborate: How would you use that method to solve this problem?Fleece

© 2022 - 2024 — McMap. All rights reserved.