UIView Hide/Show with animation UIView Hide/Show with animation ios ios

UIView Hide/Show with animation


In iOS 4 and later, there's a way to do this just using the UIView transition method without needing to import QuartzCore. You can just say:

Objective C

[UIView transitionWithView:button                  duration:0.4                   options:UIViewAnimationOptionTransitionCrossDissolve                animations:^{                     button.hidden = YES;                }                completion:NULL];

Swift

UIView.transition(with: button, duration: 0.4,                   options: .transitionCrossDissolve,                   animations: {                 button.hidden = false              })

Previous Solution

Michail's solution will work, but it's not actually the best approach.

The problem with alpha fading is that sometimes the different overlapping view layers look weird as they fade out. There are some other alternatives using Core Animation. First include the QuartzCore framework in your app and add #import <QuartzCore/QuartzCore.h> to your header. Now you can do one of the following:

1) set button.layer.shouldRasterize = YES; and then use the alpha animation code that Michail provided in his answer. This will prevent the layers from blending weirdly, but has a slight performance penalty, and can make the button look blurry if it's not aligned exactly on a pixel boundary.

Alternatively:

2) Use the following code to animate the fade instead:

CATransition *animation = [CATransition animation];animation.type = kCATransitionFade;animation.duration = 0.4;[button.layer addAnimation:animation forKey:nil];button.hidden = YES;

The nice thing about this approach is you can crossfade any property of the button even if they aren't animatable (e.g. the text or image of the button), just set up the transition and then set your properties immediately afterwards.


UIView animated properties are:

- frame- bounds- center- transform- alpha- backgroundColor- contentStretch

Describe in: Animations

isHidden is not one of them, so as I see it the best way is:

Swift 4:

func setView(view: UIView, hidden: Bool) {    UIView.transition(with: view, duration: 0.5, options: .transitionCrossDissolve, animations: {        view.isHidden = hidden    })}

Objective C:

- (void)setView:(UIView*)view hidden:(BOOL)hidden {    [UIView transitionWithView:view duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^(void){        [view setHidden:hidden];    } completion:nil];}


To fade out:

Objective-C

[UIView animateWithDuration:0.3 animations:^{    button.alpha = 0;} completion: ^(BOOL finished) {//creates a variable (BOOL) called "finished" that is set to *YES* when animation IS completed.    button.hidden = finished;//if animation is finished ("finished" == *YES*), then hidden = "finished" ... (aka hidden = *YES*)}];

Swift 2

UIView.animateWithDuration(0.3, animations: {    button.alpha = 0}) { (finished) in    button.hidden = finished}

Swift 3, 4, 5

UIView.animate(withDuration: 0.3, animations: {    button.alpha = 0}) { (finished) in    button.isHidden = finished}

To fade in:

Objective-C

button.alpha = 0;button.hidden = NO;[UIView animateWithDuration:0.3 animations:^{    button.alpha = 1;}];

Swift 2

button.alpha = 0button.hidden = falseUIView.animateWithDuration(0.3) {    button.alpha = 1}

Swift 3, 4, 5

button.alpha = 0button.isHidden = falseUIView.animate(withDuration: 0.3) {    button.alpha = 1}