Collapsible NSBox'es stacked vertically

Discussion in 'Mac Programming' started by HerQ, Oct 21, 2008.

  1. HerQ macrumors newbie

    Jun 11, 2008
    The Netherlands
    Well, I want to implement some Collapsible NSBox'es which will be able to stack up vertically, without margin. I have been watching the sample code, and it's actually the thing I'd like to have.

    My Collapsible Box class is working properly. It resizes the frame using NSAnimation nicely but there the problem begins: I have some more Collapsible NSBox'es below the perticular Collapsible NSBox, and I want them to stay just below the perticular NSBox, without margin. Now it just stays where I initially set it's frame origin to, which is not quite right because the NSBox above it became less in height.

    Since Leopard doesn't support palettes anymore, I can't open the Reducer's .nib file. I can't really get into the way Apple did do this and I've been searching for quite a long time now, but nothing found yet.

    I think it's possible to do this without an additional amount of code, since I can't find any of this this in Apple's example.

    Thanks in Advance! Some images to make it more clear:

    All Opened: Good

    Some Collapsed: Problem, the 'End Date' box needs to be below the 'Start Date' box, without margin.
  2. kainjow Moderator emeritus


    Jun 15, 2000
    If you're not using NSViewAnimation you should be as this simplifies animating views (it's a subclass of NSAnimation). I think all you need to do is move the boxes below up via their frame.origin.y value by the height of the first box. You can do this all simultaneously with NSViewAnimation.
  3. HerQ thread starter macrumors newbie

    Jun 11, 2008
    The Netherlands
    Thanks for your reply.

    I was already using the NSViewAnimation class, but you pointed me in the right direction:

    id box;
    NSMutableArray *animationArray = [NSMutableArray arrayWithObject:[NSDictionary dictionaryWithObjectsAndKeys:self, NSViewAnimationTargetKey, [NSValue valueWithRect:newFrame], NSViewAnimationEndFrameKey, nil]];
    NSEnumerator *iterator = [[[self superview] subviews] objectEnumerator];
    float yOffset = COLLAPSED_HEIGHT == newFrameHeight ? previousHeight - COLLAPSED_HEIGHT : -newFrameHeight + COLLAPSED_HEIGHT;
    BOOL flipped = [[self superview] isFlipped];
    while(box = [iterator nextObject]) {
    	NSRect frame = [box frame];
    	if(box != self && [box isKindOfClass:[NSBox class]] && ((flipped && frame.origin.y > [self frame].origin.y) || (!flipped && frame.origin.y < [self frame].origin.y))) {
    		frame.origin.y += flipped ? -yOffset : yOffset;
    		[animationArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:box, NSViewAnimationTargetKey, [NSValue valueWithRect:frame], NSViewAnimationEndFrameKey, nil]];
    This works beautifully, but I still think it's possible without any code. But whatever, this works great!


Share This Page