Bring a subview to the top on mouseDown: AND keep receiving events (for dragging)

Posted by d11wtq on Stack Overflow See other posts from Stack Overflow or by d11wtq
Published on 2011-01-01T12:50:26Z Indexed on 2011/01/01 12:53 UTC
Read the original article Hit count: 287

Filed under:
|
|
|
|

Ok, basically I have a view with a series of slightly overlapping subviews. When a subview is clicked, it moves to the top (among other things), by a really simple two-liner:

-(void)mouseDown:(NSEvent *)theEvent {
  if (_selected) { // Don't do anything this subview is already in the foreground
    return;
  }
  NSView *superview = [self superview];
  [self removeFromSuperview];
  [superview addSubview:self position:NSWindowAbove relativeTo:nil];
}

This works fine and seems to be the "normal" way to do this.

The problem is that I'm now introducing some drag logic to the view, so I need to respond to -mouseDragged:. Unfortunately, since the view is removed from the view hierarchy and re-added in the process, I can have the view come to the foreground and be dragged in the same mouse action. It only drags if I leave go of the mouse and click on the view again, since the second click doesn't do any view hierarchy juggling.

Is there anything I can do that will allow the view to move to the foreground like this, AND continue to receive the subsequent -mouseDragged: and -mouseUp: events that follow?

I started along the lines of thinking of overriding -hitTest: in the superview and intercepting the mouseDown event in order to bring the view to the foreground before it actually receives the event. The problem here is, from within -hitTest:, how do I distinguish what type of event I'm actually performing the hit test for? I wouldn't want to move the subview to the foreground for other mouse events, such as mouseMoved etc.

© Stack Overflow or respective owner

Related posts about cocoa

Related posts about osx