Cocoa: how to convert a drag and drop image when dragging

In the Builder.app interface (and some other cocoa applications), dragging an image has a very nice / sexy effect of transforming a drag and drop image when you drag and drop a dragged item from your window.

For example, in the Buildler.app interface:

  • Show library palette (β‡§βŒ˜L, or the Tools menu β†’ Library)
  • Drag an item from the Library palette

NOTE. When you drag an item from the library palette window, it is converted from the image of the original list item to the icon image of the item to be dragged.

I completely performed drag and drop in my application using the usual cocoa NSDragSource / NSDragDestination tools.

However, I cannot find a hook to perform this morphing the image while dragging. I return the original drag and drop image by overriding

-[NSView dragImage:at:offset:event:pasteboard:source:slideBack:] 

But this is only called at the beginning of the drag.

How do you signal that you want to replace the current drag and drop image (ideally, using the effect of a sexual wrinkle).

+6
objective-c cocoa drag-and-drop macos
source share
4 answers

You beat me. :-)

Yes, JLNDragEffectManager is open source (with attribution in your applications, please) and is available on my blog. It should work fine, because - no change until 10.5, but I'm not sure about what to do. Others are related to it (and it’s easily googleable), so in order to avoid self-praising blog linking, I will leave it to that.

Problems: One developer commented (and sent the corrected code) on the lack of support for drag and drop. I just did not post updates. This is the only problem that I know of.

Improvements: I would like to add several "zones" (say, one per document, so dragging from a doc to a doc document keeps the table rows similar to the table rows, but anywhere outside the doc window they are in the file icon a la HFS Promise Drag). Someday...

Design:. The record itself details the arguments for the design and the relatively simple morphing effect (cross-fade plus size is animated using basic NSAnimation, etc.). The code (class, as well as the demo application) is carefully blocked and commented on.

Will not link to my own post, but I will like the karma of clearances for my efforts .; -)

UPDATE Similar (but better integrated) functionality is available from 10.7. If you focus on 10.7 or higher, it is better to use the new API. JLNDragEffectManager works fine on 10.7, so it can be used for versions with an earlier version.

+12
source share

JNLDragEffectManager does just that. :)

+5
source share

The API does not support this. Joshua Nozzi gives a method that looks reasonable in this blog post .

The IB effect is not a fantasy. This is crossfade and scale. Hold the shift to see it more clearly.

+3
source share

As with 10.7+, the current approach is to use

  enumerateDraggingItemsWithOptions: forView: classes: searchOptions: usingBlock: 

API on NSDraggingInfo . The documentation is really bad, but ADC samples like MultiPhotoFrame or TableViewPlayground can give a good idea of ​​how to use the new mechanism.

+2
source share

All Articles