Question

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?

Was it helpful?

Solution

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];
}];

OTHER TIPS

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

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top