james james - 2 months ago 8
iOS Question

What is the difference between setting objects to .hidden = true and setting .alpha = 0?

Just a question for general practice.

What is the difference between setting objects to .hidden = true and setting .alpha = 0?

Which is more efficient?

Plus if I have multiple objects on a view, should I just hide the view? Or hide the view and each object individually?

Answer

If you set a view to "hidden", the OS will not even try to draw it or any subviews. The drawing code for a view is roughly

if (! hidden) {
    for (view* subview in views)
        if (! subview.hidden)
            subview.draw;
    [self drawmyself];
}

So this is much quicker than setting alpha, because the graphics subsystem would have to look for that exception value alpha = 0, and probably will look at the subviews anyway.

The same goes for the responder chain, "hidden" views are not looked at at all. And hidden is much easier to handle: If you set alpha, you must remember the previous alpha value so you can restore it, or you need complicated code to calculate the new alpha value in all the places where you might want to show the view again.

Comments