Abizern
Abizern

Reputation: 150755

How can I slide a view in and out of a window in a Cocoa program

I want to have a view on a window and in response to a message (button click or menu) I want to have another view slide down above it, and have the first view resize.

I want to go from this:

**********************************
*                                *
*--------------------------------*
*|                              |*
*|        view 1                |*
*|                              |*
*--------------------------------*
*                                *
**********************************

to this:

**********************************
*                                *
*--------------------------------*
*|        view 2                |*
*--------------------------------*
*--------------------------------*
*|        view 1                |*
*--------------------------------*
*                                *
**********************************

I'm not necessarily looking for code, an idea of where to start would be appreciated.

This is for a desktop app.

Upvotes: 12

Views: 5762

Answers (4)

Joshua Nozzi
Joshua Nozzi

Reputation: 61238

Alternatively, you might try an NSSplitView...

Upvotes: 0

Craig Otis
Craig Otis

Reputation: 32104

CoreAnimation is definitely your best bet. It has been a while since I've worked with any CA code, but something like:

[UIView beginAnimations:@"slideOn" context:nil];

firstView.frame = shrunkFirstViewRect; // The rect defining the first view's smaller frame. This should resize the first view

secondView.frame = secondViewOnScreenFrame; // This should move the second view on the frame. 

[UIView commitAnimations];

Later, you could return to a single view using:

[UIView beginAnimations:@"slideOff" context:nil];

firstView.frame = normalFirstViewRect; // The rect defining the first view's normal frame. This should expand the first view.

secondView.frame = secondViewOffScreenFrame; // Move the second view off the screen

[UIView commitAnimations];

Edit: The above code is for the iPhone, I read your question a bit quick.

On the Mac, you would want to use (similarly):

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:1.0f]; // However long you want the slide to take

[[firstView animator] setFrame:shrunkFirstViewRect];

[[secondView animator] setFrame:secondViewOnScreenFrame];

[NSAnimationContext endGrouping];

Upvotes: 19

jbrennan
jbrennan

Reputation: 12003

It should be noted if you don't set a duration for the animation block, the default is about 0.25 seconds, which actually seems to work very well in most cases.

I suggest trying with that duration first whenever experimenting with CoreAnimation.

Upvotes: 2

mouviciel
mouviciel

Reputation: 67889

I have never tried, but I think that CoreAnimation has interesting features for this. You have to animate height of view1 from full height to half height and position of view2 from outside its superview to top half of it.

Upvotes: 1

Related Questions