Re: reordering of subviews ?
Re: reordering of subviews ?
- Subject: Re: reordering of subviews ?
- From: Andrew Platzer <email@hidden>
- Date: Wed, 28 Nov 2001 12:56:11 -0800
On Wednesday, November 28, 2001, at 12:35 , Robert Miller wrote:
I have a view which contains some subviews that can be moved /
resized, etc., when I click on one of the subviews I want it to become
the frontmost subview overlaying the other subviews if needed. I tried
various ways to do this including this code where 'self' is the subview
inside of its mouseDown method..
[self retain];
[self removeFromSuperview];
[[self superview] addSubview:self];
the subview gets placed in an odd origin and i'm not sure if it is
really a subview again. it is not entirely visible, is there a way to
bring an NSView to the front of all other views ?
Not easily but then the reason for this is that you should never overlap
views. You will get redraw and hit detection problems. Views should be
nested only. Views are very big (> 140 bytes per instance), very
heavyweight, and shouldn't be used for content objects (e.g. rectangles in
a draw program.) Instead, have a single view that does drawing and hit
detection in its content.
For example, you might have a view containing an array of 'items'. You
draw method might be:
- (void)drawRect:(NSRect)rect
{
NSGraphicsContext* currentContext = [NSGraphicsContext currentContext]
;
int i, c = [items count];
for (i = 0; i < c; i++) {
MyObject obj = [items objectAtIndex:i];
NSRect frame = [obj frame];
if (NSIntersectsRect(frame, rect) {
[currentContext saveGraphicsState];
NSClipRect(frame);
[[[NSAffineTransform transform] translateXBy:NSMinX(frame)
yBy:NSMinY(frame)] concat];
[obj draw];
[currentContext restoreGraphicsState];
}
}
}
and of course, do the for loop in reverse for hit testing.
Andrew
__________________________________________________________________
A n d r e w P l a t z e r
A p p l i c a t i o n F r a m e w o r k s
A p p l e