Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to add animation to a view while changing its hidden mode i.e

my_view.hidden=YES; 

I have added a button in navigationbar. When we click on it the new view is set to be unhide. It draws at the upper of the navigation table.

like image 222
Sanchit Paurush Avatar asked May 30 '11 14:05

Sanchit Paurush


People also ask

Does UIView animate need weak self?

You don't need to use [weak self] in static function UIView. animate() You need to use weak when retain cycle is possible and animations block is not retained by self.

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] . It's best to let the OS run your animations; the animation thread does not block the main thread -- only the animation block itself.

Is UIView animate asynchronous?

UIView. animate runs on the main thread and is asynchronous.


2 Answers

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 opacity yourView.alpha = 1.0 //for 100% opacity 
like image 117
Jeremy W. Sherman Avatar answered Sep 21 '22 09:09

Jeremy W. Sherman


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 can change the alpha property of the view  }, completion: { _ in       self.yourView.isHidden = true // Here you hide it when animation done }) 
like image 30
Palyancodr Avatar answered Sep 24 '22 09:09

Palyancodr