How do I determine if a CALayer is fully covered b

2019-07-15 14:59发布

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

Eventually, a layer is completely covered by other layers. At this point, I'd like to remove the layer from memory.

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

Each layer has a rotation transform applied to it, so I cannot accurately make comparisons amongst all of the layers' frames.

1条回答
时光不老,我们不散
2楼-- · 2019-07-15 15:46

You could do a pixel-test to find out. Init a grayscale context the size of your screen (if possible, it only needs to be 1-bit, though I don't know if iOS actually supports that configuration). Fill the area with black. Fill the area your layer covers with white (you can take the layer's transform, set it as the CTM, and then fill the rect for your layer). Then iterate over all other layers and do the same thing, except filling with black again. Once that's 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 is still visible. Otherwise, it's not.

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

查看更多
登录 后发表回答