Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually fade in a newly added subview?

I want a view to fade in when being added to the stack via

[self.view addSubview:someSecondaryViewController.view];

How do I animate this call so that the view fades in (and out)?

like image 746
Moshe Avatar asked May 17 '10 00:05

Moshe


1 Answers

Set the alpha to zero before animating, then animate the alpha to one.

[fadingView setAlpha:0.0];
[containerView addSubview:fadingView];
[UIView beginAnimations:nil context:nil];
[fadingView setAlpha:1.0];
[UIView commitAnimations];

Before removing the view, just animate the alpha back to zero.

BTW, the view hierarchy is more of a tree than a stack.

Edit:

If you have no other cleanup after the animation ends when fading out the view, then use:

[UIView setAnimationDelegate:fadingView];
[UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];

If you are already setting a didStopSelector then call removeFromSuperview there.

like image 117
drawnonward Avatar answered Oct 20 '22 04:10

drawnonward