What is the best way to hide CALayer (without animation)?

I have several full-screen CALayers as part of a single UIView. Depending on how the user interacts with the view, I need to show one layer and hide all the others. I am currently doing this by changing the opacity, i.e.

Hide layer: [layer setOpacity: 0]; Layer for display: [layer setOpacity: 1];

For some reason, I donโ€™t quite understand, it seems to create a flashing effect on the screen. Partly to avoid this, but also because I get the impression that changes in opacity can affect performance, I wonder if changing opacity is actually the best way to hide and / or show CALayers, for example. should I change zPosition or change its position so that it no longer appears on the screen.

I donโ€™t want to animate the transition by the way.

Thanks in advance for any pointers or help.

+8
ios calayer
source share
2 answers

The usual way to hide a layer is to set its hidden property to YES , but it is not harmful to set its opacity to 0.0 to achieve this, which depends on your usage scenario.
If your CALayer not your UIView base layer ( UIView instance layer property), changing the opacity or hidden properties will activate the default animation. To prevent this, add this code before changing these properties:

 [CATransaction setDisableActions:YES]; 
+21
source share

CALayer has the "hidden" property, try setting it to YES and NO instead of switching opacity.

Hide layer: [layer setHidden: YES];

Layer to display: [layer setHidden: NO];

Hope this helps you.

+5
source share

All Articles