varun
varun

Reputation: 497

transition effect when moving from one view to another

How to add a transition effect when moving from one view to another?

e.g. when addSubView is called on the click of the button, a new view is loaded. I want a transition effect/animation like 'slide in' when doing so? Also, how to add a 'slide out' transition when returning to the original view?

Please help

Upvotes: 0

Views: 1996

Answers (1)

Mick MacCallum
Mick MacCallum

Reputation: 130183

This can be accomplished with simple UIView animations. Here's a couple of examples.

This will animate alpha from 0.0 to 1.0 giving a fade in effect:

UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[secondView setAlpha:0.0];
[self.view addSubview:secondView];

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    [secondView setAlpha:1.0];
}completion:^(BOOL done){
    //Some completion handler!
}];

And then you can dig into CGAffineTransforms to handle moving the view around. This example will move the new view in from the bottom left corner of the screen.

UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(320, 480, 320, 480)];
[self.view addSubview:secondView];

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    [secondView setTransform:CGAffineTransformMakeTranslation(-320, -480)];
}completion:^(BOOL done){
    //Some completion handler!
}];

Using blocks like these, you can pretty much make any simple to intermediate animation you want.

Upvotes: 1

Related Questions