views:

513

answers:

3

In Interface Builder.app (and some other cocoa apps), image dragging has a very nice/sexy effect of morphing the drag image while you drag a draggable item out of its window.

For example in Interface Buildler.app:

  1. Show the Library Palette (⇧⌘L, or Tools Menu -> Library)
  2. Drag an item out of the Library palette

NOTE: as you drag the item out of the Library Palette window, it morphs from an image of the original list item to an image of the icon of the dragged item.

I have fully implemented drag and drop in my Application using the normal Cocoa NSDragSource/NSDragDestination facilities.

However, I can't find a hook for doing this image morph while dragging. I'm returning the initial drag 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 would like to replace the current drag image (ideally using the sexy morph effect).

+2  A: 

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

IB's effect isn't that fancy. It's a crossfade and scale. Hold down shift to see it more clearly.

Ken
+5  A: 

JNLDragEffectManager does exactly that. :)

+6  A: 

You guys beat me to it. :-)

Yes, JLNDragEffectManager is open source (with attribution in your apps, please) and available on my blog. It should work fine as-is with no modification back to 10.5, but I'm not sure back any further. Others linked to it (and it's easily googleable), so to avoid self-congratulatory blog linking, I'll leave it at that.

Issues: One developer commented on (and submitted code to fix) the lack of dragging offset support. I've just not gotten around to posting the update. That's the only outstanding issue I'm aware of.

Improvements: I'd like to add multiple "zones" (say, one per document, so dragging from doc to doc keeps table rows looking like table rows, but anywhere outside doc windows turns them into a file icon a la HFS Promise Drag). Some day ...

Design: The post itself details the reasoning behind the design and the relatively simple morphing effect (cross-fade plus size are animated using basic NSAnimation, etc.). The code (the class as well as the demo app) is thoroughly blocked out and commented.

Won't link to my own post but would love the karma of upvotes for my effort. ;-)

Joshua Nozzi
Many many thanks Joshua. This is working beautifully. Also, great blog post! :)Don't suppose you have a link to the afore-mentioned fix for the dragoffset?
Todd Ditchendorf
If you end up using this class, just let me know and I'll add your app to the list on the source page.
Joshua Nozzi