简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM