0

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

varun
  • 465
  • 6
  • 23

1 Answers1

1

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.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • Hi, thanks...i wish to do the same across all views of my app. How can it be done once for the entire app rather than doing it for seperately for each view? – varun Aug 29 '12 at 03:35