Search code examples
objective-ccocoacore-animationnssplitview

Replacing a NSSplitView subview with Core Animation


I'm trying to figure out how to animate switching (replacing) subviews in a vertically-configured, 2-view NSSplitView. I've got it semi-working using the following methods in my NSSplitView subclass:

To set up the animation:

- (void)awakeFromNib {
    // set delegate
    [self setWantsLayer:YES];
    CATransition *transition = [CATransition animation];
    [transition setType:kCATransitionPush];
    [transition setSubtype:kCATransitionFromBottom];
    [transition setDuration:1.0];
    [self setAnimations:[NSDictionary dictionaryWithObject:transition
                                                    forKey:@"subviews"]];
}

And to perform it:

- (void)replaceRightView:(NSView *)newView animated:(BOOL)animate {
    NSRect currentSize = [[[self subviews] objectAtIndex:1] frame];
    [newView setFrame:currentSize];

    if (animate) {
        [[self animator] replaceSubview:[[self subviews] objectAtIndex:1]
                                   with:newView];
    } else {
        [self replaceSubview:[[self subviews] objectAtIndex:1]
                        with:newView];
    }
}

However, this code has the effect of pushing the entire NSSplitView off, rather than just the subview on the right side of the split.

Is there a way to animate just the subview transition? Perhaps I'm using the wrong animation key ("subviews")? Other animation methods would be fine too.

Thanks!


Solution

  • It's probably not the cleanest way, but I ended up using an NSView 'container' subclass with custom addSubview: and replaceSubview:withView: methods that modify the new subview's frame to match the container view's frame, which is then structured into the NSSplitView subview I wanted to animate. I then setup the CATransition on the container view, and everything worked as I wanted.