Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: reordering of subviews ?



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


References: 
 >reordering of subviews ? (From: Robert Miller <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.