ios - CALayer draw outside of rect -
i'm using code add layer custom uiview , works charm:
cgrect newrect = cgrectmake(0, 0, self.bounds.size.width, self.bounds.size.height); calayer* heartbackground = [calayer layer]; heartbackground.contents = (__bridge id)([uiimage imagenamed:@"5heartsgray"].cgimage); heartbackground.frame = newrect; [self.layer addsublayer:heartbackground];
but when tried use in draw method using quartz using new rect(like this)
cgcontextsavegstate(context); cgrect ratingrect = cgrectmake(250, 100, 150, 20); calayer* heartbackground = [calayer layer]; heartbackground.contents = (__bridge id)([uiimage imagenamed:@"5heartsgray"].cgimage); heartbackground.frame = ratingrect; [heartbackground renderincontext:context]; cgcontextrestoregstate(context);
it renders in beginning of frame , not inside of ratingrect.
if call [heartbackground setneedsdisplay]
disappear. same thing heartbackground.maskstobounds = yes
what i'm doing wrong. need switch cglayer because i'm using coregraphics?
this output when working coregraphics (as see hearts starts @ coordinates x=0, y=0 , starts @ x=250, y=100):
it doesn't make sense create , draw calayer
inside draw function of uiview
. calayer
independently drawable unit , draws on parentlayer's context default. means if want use separate calayers try adding many sublayers self.layer (inside uiview
) want, set frames , they'll drawn when container view shows up. regarding problem, here's can try.
option 1: take code
cgrect ratingrect = cgrectmake(250, 100, 150, 20); calayer* heartbackground = [calayer layer]; heartbackground.contents = (__bridge id)([uiimage imagenamed:@"5heartsgray"].cgimage); heartbackground.frame = ratingrect; [self.layer addsublayer:heartbackground];
out of drawrect:
method , write in initwithframe:
, in drawrect [heartbackground renderincontext:uigraphicsgetcurrentcontext];
option 2: draw hearts without calayer.
cgrect ratingrect = cgrectmake(250, 100, 150, 20); [[uiimage imagenamed:@"5heartsgray"] drawinrect:ratingrect];
Comments
Post a Comment