Re: Request for guidance re. CALayer
Re: Request for guidance re. CALayer
- Subject: Re: Request for guidance re. CALayer
- From: Corbin Dunn <email@hidden>
- Date: Wed, 07 Jan 2015 10:39:51 -0800
> On Jan 7, 2015, at 7:02 AM, Luc Van Bogaert <email@hidden> wrote:
>
> Hello,
>
> I’ve been working on a drawing app for OS X in Objective-C for personal educational purposes. Thanks to the help I’ve received from various people in this list, this is coming along very nicely, but now I am once again calling for your help on a more complex subject.
>
> My aim for this app is to provide some kind of “layer” functionality. I’ve already implemented a Layer class as an NSObject subclass, and I also have a panel to manage Layer objects, including creation, deletion, renaming, reordering, editing various attributes (locked, hidden, etc.).
>
> I also have implemented a custom view class CanvasView representing the app’s drawing canvas. I realise I could do all the drawing using this view’s drawRect: method. But somehow I keep having this feeling that I should pursue the usage of CALayer objects, possibly using them to form a composite object as part of my Layer class. What I have been trying to do so far is adding a CALayer property to my Layer class, so I can add a real CALayer object as a sublayer in my custom CanvasView reating a one-on-one relation between my Layers (that would contain the graphics data) and real CALayer objects (to display the graphics). I have assigned the Layer object as the CALayer object’s delegate…
> I’m exactly not sure why, but to me this seems to be a good strategy for performance considerations and possibly offering other advantages later on...
>
> However, I am not at all sure if this is the best route to take, so I would welcome any help or recommendations on this.
>
> Wether or not this is the best approach for my drawing app, I could also use some basic expert advise on Core Animation programming in general, and specifically on the usage of the CALayer class. I’ve read some of Apple’s docs and I will read them again and again, but maybe someone here could summarise some important basic concepts for me or touch on some misconceptions that beginners should watch out for.
>
> For instance, I have already been able to create and add sublayers to a layer-backed view, but it is still unclear to me how to draw graphic content in these layers. I know that there are various methods, but I still don’t have a clear picture of how layers generally fit together with custom views. I’ve read about the CALayer delegate property and the drawLayer:inContext: method, but I’m not sure who, when or how this method should be called.
>
> Thanks for any help,
Hi Luc,
You don’t need to go directly to using CALayers, and in fact, I don’t recommend this. You can still get the power of CoreAnimation by using layer-backed NSViews. On your view that contains the majority of your subviews (i.e.: your “drawing layers”), call setWantsLayer:YES. All subviews will now be layer-backed.
Each virtual layer that you want the user to draw into should be a custom NSView subclass. Each of these views should override drawRect: and implement the drawing for that particular layer.
Under the hood, AppKit sets the delegate and implements drawLayer:inContext:, and call your NSView’s drawRect:.
If you need to access specific CALayer properties, ideally use the NSView cover methods (such as compositingFilter, backgroundFilters, etc), but you can fallback to directly setting the view.layer property yourself. Just be careful, as AppKit “owns” certain properties (frame, bounds, position, flipped, etc).
-corbin
>
> --
> Luc Van Bogaert
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden