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

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


Animate the view's opacity from 100% to 0%. Have the animation completion callback set the view to be hidden. You might also want to reset the opacity back to 100% during the callback, so the view will display fully opaque when you unhide it.

yourView.alpha = 0.0 //for zero opacityyourView.alpha = 1.0 //for 100% opacity


There is no animation for hiding however; you get the same result with the Swift code below:

UIView.animate(withDuration: 0.2, delay: 0, options: [], animations: {                self.yourView.alpha = 0 // Here you will get the animation you want}, completion: { _ in      self.yourView.hidden = true // Here you hide it when animation done})


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