11

Coming from ActionScript, I would set Sprites to visible=false in order to keep them from being calculated in things like layout, and to ensure they would not respond to events.

In iOS development, I am continuing with this--if a UIView is not needed, I may both animate its alpha to zero and then set hidden=true. I wanted to know if I was wasting my time, or if there is a benefit to this. In my current project, I'm doing so with UIImageViews which are not responding to events anyway.

Is setting hidden to true good practice, or or just additional overhead?

akaru
  • 6,299
  • 9
  • 63
  • 102

2 Answers2

22

This is the best choice, because setting hidden to true removes view from the render loop. While setting alpha to 0 just makes view transparent.

Max
  • 16,679
  • 4
  • 44
  • 57
  • yes, that's what I was curious about. So it will give a bit of a performance "boost" by being set to hidden. Thank you. – akaru Feb 17 '11 at 04:35
  • I'm looking for the source if this - can you point me to a url? – steipete Sep 21 '11 at 22:07
  • @steipete it's actually not clear what you mean under sources? – Max Sep 21 '11 at 23:23
  • "This is the best choice, because setting hidden to true removes view from the render loop. While setting alpha to 0 just makes view transparent." - where is this from? Some Apple document? I was searching for this but couldn't find a source other than StackOverflow. – steipete Sep 22 '11 at 14:13
  • 3
    making alpha = 0.0 also makes it hidden = YES under the hood, so it would also remove it from the render loop ;) – Javier Soto Jan 13 '12 at 15:02
  • 2
    @Javy Actually, Apple specifies that as soon as alpha drops under (on top of my head) either .1 or .05, objects will not respond to touch events anymore. – Tom Jan 25 '13 at 00:24
6

If you don't need it any more, then you should properly remove it from memory. In that case you would just animate its alpha (to make it look good), then dealloc it.

if you autoreleased it, then all you have to do is remove it from the superview and its retain will hit 0 and be dealloced.

[UIView animateWithDuration:.5
                 animations: ^ {
                      [myView setAlpha:0];
                 }
                 completion: ^ (BOOL finished) {
                      [myView removeFromSuperview];
                 }];
ColdLogic
  • 7,206
  • 1
  • 28
  • 46
  • Good point. In my case, it is a small UIImageView that is used frequently, which happens to be overlaying some opengl content. So, drawing update performance is more important than memory in this case. However, I'll take this in to consideration. – akaru Feb 17 '11 at 06:02
  • Yea, if you're going to continuously use it, just hide it like previously stated until you are done with it :P – ColdLogic Feb 19 '11 at 00:05
  • I couldn't. However I'm not trying to do this anymore. Thanks for replying. – Kio Coan Jun 05 '19 at 02:22