cocoa - drawLayer not called when subclassing CALayer -
i have simple calayer subclass (boxlayer) drawlayer method:
- (void)drawlayer:(calayer *)layer incontext:(cgcontextref)ctx { nslog(@"drawlayer"); nsgraphicscontext *nsgraphicscontext; nsgraphicscontext = [nsgraphicscontext graphicscontextwithgraphicsport:ctx flipped:no]; [nsgraphicscontext savegraphicsstate]; [nsgraphicscontext setcurrentcontext:nsgraphicscontext]; // ...draw content using ns apis... nspoint origin = { 21,21 }; nsrect rect; rect.origin = origin; rect.size.width = 128; rect.size.height = 128; nsbezierpath * path; path = [nsbezierpath bezierpathwithrect:rect]; [path setlinewidth:4]; [[nscolor whitecolor] set]; [path fill]; [[nscolor graycolor] set]; [path stroke]; [nsgraphicscontext restoregraphicsstate]; }
i have awakefromnib in nsview subclass:
- (void)awakefromnib { calayer* rootlayer = [calayer layer]; [self setlayer:rootlayer]; [self setwantslayer:yes]; box1 = [calayer layer]; box1.bounds = cgrectmake(0, 0, 70, 30); box1.position = cgpointmake(80, 80); box1.cornerradius = 10; box1.bordercolor = cgcolorcreategenericrgb(255, 0, 0, 1); box1.borderwidth = 1.5; [rootlayer addsublayer:box1]; box2 = [boxlayer layer]; [box2 setdelegate:box2]; [box2 setneedsdisplay]; [rootlayer addsublayer:box2]; }
my drawlayer never called though, why not?
thank you
possibly because frame of layer seems equal cgrectzero
, os might think it's invisible doesn't need draw it.
on side note, why going complicated way of setting layer's delegate , implementing drawlayer:incontext:
instead of using drawincontext:
directly?
Comments
Post a Comment