Search code examples
iosuipickerview

How do I animate my subviews on adding them?


I have developed a font selection that utilizes UIPickerView and UIToolbar, which are both added on touching a UIButton.

But they just kind of pop in which looks sloppy.

is there a way to animate them?

here is the method that adds them (it's called on clicking the button) :

-(void)showPicker
{
    [self.view addSubview:_fontPicker];
    [self.view addSubview:pickerTB];

}

Solution

  • You can animate them in a couple of ways, but probably the easiest is to just fade them in.

    Fade In

    [someView setAlpha:0]
    [self.view addSubview:someView];
    [UIView beginAnimations:@"FadeIn" context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [someView setAlpha:1];
    [UIView commitAnimations]; 
    

    Fade Out

    float fadeDuration = 0.5;
    [UIView beginAnimations:@"FadeOut" context:nil];
    [UIView setAnimationDuration:fadeDuration ];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [someView setAlpha:0];
    [UIView setAnimationDidStopSelector:@selector(removeView)];
    [UIView commitAnimations];
    
    
    -(void) removeView {
        [someView removeFromSuperview];
    }
    

    Something as simple as that.