2

I have an app that lays out pages for a magazine. The app lays out pages (NSViews of a class called Page) of a magazine on a NSView called MasterPage (which itself is the contentView of an NSScrollView). Think of a grid of pages in a scroll view.

The "pages" then have subviews auto placed on them that represent ads (class = Ad). I want to be able to drag these ads within their superview (an NSView of the Page class) and between subviews of MasterPage (the other Page class views).

I can drag the Ad class NSView on the Page class NSView it was originally placed on by invoking the mouseDragged method but how can I "release" the Ad class subview from the superview and drag it to another NSView?

Hopefully that made sense.

PruitIgoe
  • 6,166
  • 16
  • 70
  • 137

1 Answers1

0

When you begin a drag event, remove the dragging view from its superview, and add it to the contentView of the NSWindow. You can then drag it around the entire window. When the drag event completes, use hitTest: to determine what view you're on top of (you may have to walk back up the superview hierarchy to find the view you really want). Then add the dragging view back to the final target.

Depending on how you've implemented clipping in your views, you may be able to skip the step of moving the dragging view to the contentView. If you don't clip your page views, then it's ok for the dragging view to move all over the screen.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • 1
    Thanks Rob, I was going in that direction. I can get it out of the superview and into the contentView but the drag becomes disabled – PruitIgoe Oct 25 '11 at 17:38
  • I'm not sure this will work (but I've never tried it). I suspect it's because the view being removed from the view hierarchy is screwing up the dragging machinery. In this case, I'd recommend leaving the view in place but having it redraw itself as blank while "in drag" and only complete the view move upon successful drop. – Joshua Nozzi Oct 25 '11 at 18:10
  • You will likely need to track mouseDown, mouseMoved and mouseUp manually. It's been a while since I've done this. To Joshua's point, you may want to render your view into a bitmap, hide the view, and drag the bitmap. That can be good for performance and for applying effects (this is how drag-and-drop is generally implemented). – Rob Napier Oct 25 '11 at 19:41
  • Hiding is probably easier. :-) Unless you've got other things showing/hiding the view, then it could get tricky. Otherwise, just draw a blank view while in drag. – Joshua Nozzi Oct 25 '11 at 19:47