tags:

views:

592

answers:

1

Hi,

I have added a button on a view(the view is of the same size as the button). When that button is clicked a new view has to be displayed. So in the button's event handler I have added the newview as a subview of the view on which the button is added, so that the newview gets displayed when the button is clicked. The thing here I need to do is, when I click on the button, the newview has to be invoked from top to bottom (it has to look like it slides down from the button's view). When the same button is clicked again, the newview has to scrollback(slide) and disappear. I know that I need to apply some animation stuff to get this effect. But I have no idea how to do this. Can you please help me giving some ideas.

Thanks in advance!!!

+1  A: 

Well you can animate the size of your window to simulate the slide from top like this:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
myView.frame = CGRectMake(0, buttonHeight+buttonTop, myView.frame.size.width, viewTargetHeight);
[UIView commitAnimations];

Make sure you set the view height to 0 when you create it.

To scroll it back up just do the opposite

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
myView.frame = CGRectMake(0, buttonHeight+buttonTop, myView.frame.size.width, 0);
[UIView commitAnimations];

If you actually want your new view to slide you can try animating both the size and the position and you'll also probably need to clip your animated view with another view.

Ron Srebro
Thanks,This works fine.But I added a button in myView.When we expand the view it works fine,but when we slide it back, the button's title scrolls upwards and stays there itself.It doesn't vanish fully.
saikamesh
I'm not sure I fully understand what is wrong but try setting mvView.clipsToBounds to YES. might solve it.
Ron Srebro
This is not how to slide a view in, here you are animating the height, not the y coordinate of the origin. This makes it animate growing from 0 height to normal height. Better solution is to animate the y coordinate, start it off screen then move it into position.
progrmr
@kk6yb In order to animate only the y position, something needs to hide the view when it's in the higher position. I also stated in my answer that in order to perform a true slide, he'll need to animate the y coordinate and clip the view, but in most cases the much easier to implement resizing will look almost identical.
Ron Srebro