Mindi Hansen Mende
Mindi Hansen Mende

Reputation: 195

Add a subview to a view more beautifuly?

I want to be able to fade in a sub view. Is there a way to animate that so that when my subview gets added it fades in and not just is all of a sudden pops up there. I know I could get several instances of my imageview with different alphas and then animate it that way but isn't there an easier way?

Upvotes: 2

Views: 821

Answers (3)

rickerbh
rickerbh

Reputation: 9911

Yes, you can animate the view without needing different images. The below code will fade your view in over 0.3 seconds.

[myView setAlpha:0.0];
[myView setHidden:NO];
[UIView animateWithDuration:0.3 animations:^{
  [myView setAlpha:1.0];
}];

Upvotes: 5

jpros
jpros

Reputation: 323

You can set the alpha to 0 before adding the subview, and after your addSubview, you make an animation, like that:

yourView.alpha = 0.0f;
[self.view addSubview:yourView];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
yourView.alpha = 1.0f;
[UIView commitAnimations];

Upvotes: 3

justin
justin

Reputation: 104698

You need only one instance. UIView.alpha can be animated.

Upvotes: 4

Related Questions