Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView: Performance/memory difference between alpha:0, hidden:YES, removeViewFromSuperview and frame.origin.y = -100000;

What is best for the device when temporarily hiding a view (e.g. until loading is done)?

  • Setting alpha to 0?
  • Setting hidden to YES?
  • Removing view from superview?
  • Moving the view to a position which makes it not possible to be visible on window/screen (e.g. frame.origin.y = -10000)?

Which is best in terms of memory and which is best in terms of performance? I know Apple somewhere wrote something about this, but I can't find it.

like image 749
hfossli Avatar asked Dec 26 '22 13:12

hfossli


2 Answers

Which is best in terms of memory

removeFromSuperview is the best in term of memory. Why ? Because it will cause the view to be released. So if the view is not retained by anyone else (like a strong ivar), it will be deallocated.

And which is best in terms of performance?

setHidden: is the best in terms of performance. Why ? Because the action is just to set a flag. And then in drawRect it's just a BOOL check. So it's really fast because there is no other action involved.

like image 55
Francescu Avatar answered Dec 28 '22 09:12

Francescu


So, the fastest method first:

  1. setHidden (discard view from rendering only)
  2. alpha to 0 is Equal to setHidden, but it first checks if0`
  3. removeFromSuperView (need some steps: deallocate object, pop from stack (pointer), remove from rendering stack...)
  4. frame.origin.y=-10000 > must move a whole bit array and check if the view comes out of bounds. not very fast...
like image 31
Mirko Brunner Avatar answered Dec 28 '22 10:12

Mirko Brunner