This is how I switch from one UIView to another UIView: self.view = MyView;
How would I fade out a view to my MySecondView
?
Asked
Active
Viewed 2,615 times
2

Daniyar
- 2,975
- 2
- 26
- 39

atomikpanda
- 1,845
- 5
- 33
- 47
2 Answers
6
You can use UIView's build in transition code to cross dissolve between them.
[UIView transitionFromView:self.view toView:MySecondView duration:0.25 options:UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished) {
// What to do when its finished.
}];
You can also use the regular animation options with something like this. I haven't tested this code as I'm not in front of Xcode but I use it all the time for labels and things without default animations.
[UIView animateWithDuration:0.25 animations:^{
[self.view setAlpha:0.0];
self.view = mySecondView;
[self.view setAlpha:1.0];
}];

Ryan Poolos
- 18,421
- 4
- 65
- 98
-
It's exactly what I wanted, but how would I fade it back into `MyFirstView`? – atomikpanda Aug 07 '12 at 14:51
-
Just reverse the code? Or if you mean like right away you can do `UIViewAnimationOptionAutoReverse`. Im not super clear on what you mean. – Ryan Poolos Aug 07 '12 at 15:11
-
I have tried `[UIView transitionFromView:self.view toView:MyFirstView duration:3 options:UIViewAnimationOptionTransitionCrossDissolve completion:^(BOOL finished) { // What to do when its finished. }];` But nothing happens. – atomikpanda Aug 07 '12 at 15:38
-
I fixed by changing ``transitionFromView:**self.view**` to **MySecondView** – atomikpanda Aug 07 '12 at 15:40
-
Then to fade back you just do selfview to MyFirstView? – Ryan Poolos Aug 07 '12 at 15:56
2
Please note that it's not a good idea to do things like self.view = MyView to change screens. By the time you get to a few screens, your viewController will be filled with spaghetti code. You should consider presenting new view controllers that manage their views. One way you can do fade is as follows:
- Fade the current view to black (with animation)
- In the view controller that you are going to push use viewWillAppear to fade the view to black as well
- Push/Present the view controller without animations.
- Now use the viewDidAppear method of the newly presented view controller to fade in the view (with animation).

Kaan Dedeoglu
- 14,765
- 5
- 40
- 41