Search code examples
iphoneiosxcodeuiviewanimated

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?


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