ios - How to draw inside a layer and add to context only after [viewLayer addSublayer:newLayer]? -
the question
how create separate context layer , incorporate context super layer?
why want this
i want abstract drawing of view layers separate objects/files. want construct view out of layers, position on top of 1 , have other possibilities that.
the problem i'm not aware of you're supposed draw part of view layer without drawing straight context of main views sublayer.
here's example, have subclassed calayer hffoundation:
@implementation hffoundation - (void)drawlayer:(calayer *)layer incontext:(cgcontextref)ctx { uibezierpath *foundation = [uibezierpath bezierpath]; // drawing goes on here in form of messages [foundation] } @end now when instantiate custom layer inside views (void)drawrect:(cgrect)rect way:
hffoundation *foundation = [hffoundation new]; foundation.frame = cgrectmake(40, viewheight - 100, 300, 100); foundation.backgroundcolor = [[uicolor colorwithred:1 green:.4 blue:1 alpha:0.2] cgcolor]; [self.window.rootviewcontroller.view.layer addsublayer:foundation]; i result (no drawings appear, bg color inside layer frame):

if [foundation drawlayer:foundation incontext:context]; afterwards, drawing appears, appears inside top layers context, not in foundation layer. since foundation layer lower in hierarchy, hides drawing (unless reduce it's alpha, i've dont in picture):

how draw layer itself, i.e. foundation in case?
to insert sublayer , set index want piece of code:
[view.layer insertsublayer:foundation atindex:0];
Comments
Post a Comment