Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

calayer

I have a number of 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 others. I'm currently doing this by changing the opacity, i.e.

Layer to hide: [layer setOpacity:0]; Layer to show: [layer setOpacity:1];

For a reason I don't quite understand, this seems to create a flashing effect on the screen. Partially to avoid this, but also because I have the impression that opacity changes can affect performances, I'm wondering if changing opacity is actually the best way to hide and/or show CALayers, e.g. should I consider changing the zPosition or changing its position so that it no longer appears on screen.

I don't want to animate the transition by the way.

Thanks in advance for any pointers or help.

like image 929
pingin Avatar asked Aug 10 '13 23:08

pingin


2 Answers

The normal way to hide a layer is setting its hidden property to YES, but it's no harm to set its opacity to 0.0 to achieve that, which depends on your usage scenario.
If your CALayer is not your UIView's underlying layer(the UIView instance's layer property), change the opacity or hidden properties will trigger animation by default. To prevent that, add this code before changing these properties:

[CATransaction setDisableActions:YES];
like image 85
liuyaodong Avatar answered Nov 05 '22 07:11

liuyaodong


CALayer has a property called "hidden", try setting that to YES and NO instead of switching opacities.

Layer to hide: [layer setHidden:YES];

Layer to show: [layer setHidden:NO];

Hope this helps you.

like image 5
TomWildcat Avatar answered Nov 05 '22 07:11

TomWildcat