How to determine if CALayer is fully covered by other CALayers?

I have a series of layers that randomly fit on the screen. As each layer is added, it is placed on top of all the others.

In the end, the layer is completely covered with other layers. At this point, I would like to remove the layer from memory.

Is there any way to know when a layer is covered (or 100% or some fraction) with other layers?

Each layer has a rotation transform applied to it, so I can’t accurately compare between all frames of the layers.

+2
source share
1 answer

You can run a pixel test to find out. Initiate the grayscale size of your screen (if possible, it should be only 1 bit, although I don't know if iOS supports this configuration). Fill the area with black. Fill the area in which your layer is covered with white (you can accept the transformation of the layer, set it to CTM, and then fill the rectangle for your layer). Then iterate over all the other layers and do the same except refilling with black. Once this is done, you can scan all the pixels in the context, looking to see if any of them are white. If you find a white pixel, the layer will still be visible. Otherwise, it is not.

Naturally, this assumes that all your layers are completely opaque and fill all their borders.

+3
source

All Articles