Uiview Hide/Show with Animation

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.

Animation show and hide view in Swift

You need to manipulate the alpha property and not the isHidden property for animation of UIView fade.

Try the following:

func showAndHideFilterMenu(category : Int) {
if showFilterMenu == false {
self.filterView.alpha = 0.0
self.filterView.isHidden = false
self.showFilterMenu = true

UIView.animate(withDuration: 0.6,
animations: { [weak self] in
self?.filterView.alpha = 1.0
})
} else {
UIView.animate(withDuration: 0.6,
animations: { [weak self] in
self?.filterView.alpha = 0.0
}) { [weak self] _ in
self?.filterView.isHidden = true
self?.showFilterMenu = false
}
}
}

UIView show/hide animation : hide animation does not seem to be working

You can simply go with below function:

UIView.animate(withDuration: 0.3, animations: {
self.consentStatusView.alpha = 0 //If you show a view alpha = 1
}) { (finished) in
self.consentStatusView.isHidden = true //If you unHide your view isHidden = false
}

How to hide/show UIView with animation in iPhone

Your code works, i have used it. Look code below

.h file:

#import <UIKit/UIKit.h>

@interface StackoverflowTestViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *cautionView;


- (IBAction)btnToggleClick:(id)sender;
@property (strong, nonatomic) IBOutlet UIButton *btnToggle;
@end



.m file:

#import "StackoverflowTestViewController.h"

@interface StackoverflowTestViewController ()

@end

@implementation StackoverflowTestViewController

bool isShown = false;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)btnToggleClick:(id)sender {
if (!isShown) {
_cautionView.frame = CGRectMake(130, 20, 0, 0);
[UIView animateWithDuration:0.25 animations:^{
_cautionView.frame = CGRectMake(130, 30, 100, 200);
}];
isShown = true;
} else {
[UIView animateWithDuration:0.25 animations:^{
_cautionView.frame = CGRectMake(130, 30, 0, 0);
}];
isShown = false;
}
}
@end

Hide view and apply hide effect - Swift

Use it like this

        UIView.animate(withDuration: 0.5, animations: {
self.button.alpha = 0
}) { (_) in
self.button.isHidden = true
}

Hide the button after your view's alpha has changed to 0.
The issue in your code is that the button gets hidden in the animation block so the animation happens when the view is already hidden.

Making an animation to slide and hide/show an UIView(swift 5)

In the storyboard set constraints on the size of the views. Set constraints from the right side of the red view and green view from the right side of the superview they share. Define some constants for the values needed for both positions for both views.
Then something like this:

    @IBOutlet weak var greenConstraint : NSLayoutConstraint
@IBOutlet weak var redConstraint : NSLayoutConstraint

let greenSlideOutValue = -2000.0 // big enough to get green view offscreen
let redSlideInValue = 0.0 // aligns red view right edge to superview

let greenSlideInValue = 100.0 // puts green view onscreen offset from right edge
let redSlideOutValue = -2500.0 // big enough to get red view offscreen.

UIView.animate(withDuration: 0.75, delay: 1.0, options: .curveEaseOut, animations: {
greenConstraint.constant = greenSlideOutValue
redConstraint.constant = redSlideInValue
self.view.layoutIfNeeded()
}, completion: { finished in
print(“slide green out, red in”)
})

Do this in the event handler for say a tap or gesture recognizer associated with the views. Do similar for the red view event(s)

Code isn’t compiled and is just typed in but should get you started.

UIStackView Hide View Animation

Just had the same issue.
The fix is adding stackView.layoutIfNeeded() inside the animation block. Where stackView is the container of the items you're wishing to hide.

UIView.animate(withDuration: DiscoverHeaderView.animationDuration,
delay: 0.0,
usingSpringWithDamping: 0.9,
initialSpringVelocity: 1,
options: [],
animations: {
clear.isHidden = hideClear
useMyLocation.isHidden = hideLocation
stackView.layoutIfNeeded()
},
completion: nil)

Not sure why this is suddenly an issue in iOS 11 but to be fair it has always been the recommended approach.

How to add animation while changing the hidden mode of a uiview?

Unfortunately, hidden is not a property that is animatable through UIView animations. I think your best bet may be to use one of the animations @Erik B suggested, or start dabbling with Core Animations which are much more powerful. Take a glance at the documentation for UIView animations and Core Animations.

I achieved something like what your suggesting by using UIView animations to slide the new view from below another view. This made it appear like a drawer sliding out. If you want to do something like that, you need to intercept the touch up inside event and place the animation code there.

- (IBAction)buttonClicked:(id)sender {
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationCurveEaseOut
animations:^(void) {
self.myView.frame = /* set the frame here */
}
completion:NULL];
}

UIView flip transition with show hide animation iOS

Both of them its working on me. Just replace mediumFirstView and mediumSecondView location.

UIView.transition(from: mediumSecondView, to: mediumFirstView, 
duration: 5.5, options: [.transitionFlipFromRight,
.showHideTransitionViews]) { _ in
self.mediumFirstView.isUserInteractionEnabled = false
self.mediumSecondView.isUserInteractionEnabled = true
}

Sample Image
Sample Image



Related Topics



Leave a reply



Submit