UIView hidden property…is there more to it?

2019-02-03 16:45发布

问题:

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?

回答1:

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.



回答2:

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