views:

54

answers:

2

Hi,

In my test application (im learning) i have 2 view controllers. on the first view i have button "go to second view".

what i want to do : when user click the "go to second view", the first view move left and go out of the screen and the second view will appear from the right and replace the first view.

now, this animation is happen when pushing and poping with navigate controller.

my question : how can i do the same animation, without nav controller ?

+2  A: 

You can add a view off screen to the right, and then you can animate it to a new frame on-screen. Another common use for this method is to animate modal menu views from the bottom. You can also animate other properties of the view, such as the alpha value to make a view disappear/reappear.

// the size of the screen minus the Status Bar
#define SCREEN_FRAME [[UIScreen mainScreen] applicationFrame]

// add the full-screen view offscreen to the right
CGRect frame = CGRectMake(SCREEN_FRAME.size.width,
                          SCREEN_FRAME.origin.y, 
                          SCREEN_FRAME.size.width, 
                          SCREEN_FRAME.size.height);
UIView *view = [[[UIView alloc]initWithFrame:frame]autorelease];
[self.view addSubview view];

// this is the frame the view will end on after the animation
CGRect newFrame = CGRectMake(SCREEN_FRAME.origin.x,
                          SCREEN_FRAME.origin.y, 
                          SCREEN_FRAME.size.width, 
                          SCREEN_FRAME.size.height);

// animate the transition
[UIView beginAnimations:nil context: nil];
[UIView setAnimationDuration: .5];
view.frame = newFrame;
[UIView commitAnimations];
Andrew Johnson
A: 

thanks for the answer, i tried your code, but it is not the same animation like the navigate controller is doing when pushing/poping view.

yoni
I think it's the same... just change the timing. It should be faster than half a second.
Andrew Johnson