简体   繁体   中英

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 (eg 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 (eg 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.

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.

So, the fastest method first:

  1. setHidden (discard view from rendering only)
  2. alpha to 0 is Equal to setHidden, but it first checks if 0`
  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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM