views:

362

answers:

1

I want to animate a view on top of another view in my iphone app. I basically want my view to look like the apple keyboard except with my custom controls. When i click a button I want the new view to animate up, from the bottom of the screen, on top of part of the view.

How would I do this?

+3  A: 

Just add the view somewhere outside the visible frame of the super view (the view you are animating on top of), then change it's position using an animation block. It will slide up.

- (void)addView
{
    // Obviously this won't compile with cut&paste... you have
    // to supply the actual views.
    UIView* myControlView = // assume this exists somewhere
    UIView* myMainView    = // assume this exists too
    myControlView.frame   = CGRectMake(0, 480, 320, 100); // use real numbers
    [myMainView addSubview:myControlView];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    myControlView.frame   = CGRectMake(0, 380, 320, 100);
    [UIView commitAnimations];
}

There are a number of other ways to do this as well. For instance, you can have a view set exactly where you want it and then translate it off screen to "hide" it. When you want it to come back, transform it again with an affine transform inside an animation block and it will slide back up. That way works a little better with views that are pre-laid out, like using Interface Builder and such where you don't necessarily know what the positions of the frame are, or they're subject to change in other code.

Jason Coco
is there anyway i could get this to work with tableviews?? For example when i click on a row a control appears from the bottom of the screen. Using the code above on a child of UIScrollView does not work optimally.
zPesk
You have to add something like this to the view that contains the scroll view, not the scroll view itself. Realistically, the scroll view and this control are siblings, with this control being foremost when it's being used.
Jason Coco
how would you make it the foremost view???
zPesk
Adding it to the containing view will automatically make it foremost. If it's already there you would call the bringSubviewToFront: on the container view.
Jason Coco