Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subview of NSView or CALayer?

Today I decided to try to use CALayers to show a rectangular box overlaying a NSView. The layer will contain some text and will be turned on and off depending on when it is necessary to show the variable text. The reason I wanted to use CALayer for this was the nifty rendering and animation you can easily do with CALayer. I implemented my layer and it worked like a charm. However, after using my GUI and clicking several times on various buttons turning the layer on and off, it seemed that the hierarchy of what I thought was my layer view was skewed. I think focus must have been switched to some other NSView which again was turned off. I basically got very confused as to which layer I was handling at a given time and I lost control of the view hierarchy.

My question is: should I use subviews of NSView, or CALayers to show something that may occur many times on and off in an application? It seems to me that it is easy to loose control of which layer you are working on. Is there a way to identify by name the current layer so you can reuse the layer, or is it best to work with layers, delete them and then re-create the layer the next time you need them?

Thanks for your time. Cheers, Trond

like image 563
Trond Kristiansen Avatar asked Dec 29 '22 00:12

Trond Kristiansen


1 Answers

FWIW,

  1. I have often turned CALayers "on and off" very quickly, with no problems at all. So you can do that if you want to!

  2. Deleting them and recreating them quickly on the fly, does not seem to cause any problems. (It's not a big resource user, and I've never seen any other problems doing that.) So definitely do that if you want to!

  3. I actually don't understand what you mean about "naming" layers - of course, as iVars they have a name! You have to name all your CALayers.

Here's a typical bit of production code (from a .h file):

CALayer         *rearLayer;
CALayer         *hugeBasket;        // holds everything for ez-on/off
CALayer         *theActualSkyline;  // nb, same name as similar UIView
CALayer         *someTrees;         // minor stuff
CALayer         *someBushes;        // overs

// for the stupid help basket..
CALayer         *LLDRear;
CALayer         *LLDArrowLeft;
CALayer         *LLDArrowRight;
CALayer         *LLDPointlessUpArrow;
CALayer         *LLDYetAnotherStupidShadow;

// etc etc..

And so on and on. I don't really see how you can "not" name them, you know!

Finally,

4, Don't forget layers are much "better" than NSViews, because: NSViews have shoddy/buggy relationship between overlapping siblings: they essentially don't work. Read about that here: port an iOS (iPhone) app to mac?

Hope it helps!

PS - these may also help with CALayers...

Exactly what should happen in a CALayer's display/drawRect methods?

What's the difference and compatibility of CGLayer and CALayer?

like image 135
Fattie Avatar answered Jan 25 '23 07:01

Fattie