ios - UIView draw rect retain's the previous drawing and does not clear on view.transform -
i have following code show marker in uiview. marker show's well, , once try pinch zoom , scale uiview using transform first drawing remains is, after calling setneedsdisplay.
my custom uiview subclass has following code
- (void)drawrect:(cgrect)rect { // drawing code cgfloat w=20.0f; cgfloat h=8.0f; cgcontextref context=uigraphicsgetcurrentcontext(); cgcontextsetfillcolorwithcolor(context, [uicolor bluecolor].cgcolor); cgcontextclearrect(context,self.bounds); cgcontextsetstrokecolorwithcolor(context, [uicolor redcolor].cgcolor); cgcontextsetlinecap(context, 2.0); cgmutablepathref leftmarker=cgpathcreatemutable(); cgpathmovetopoint(leftmarker, null, 0, 0); cgpathaddlinetopoint(leftmarker, null, w, 0); cgpathaddlinetopoint(leftmarker,null, w, h); cgpathaddlinetopoint(leftmarker,null, h, h); cgpathaddlinetopoint(leftmarker,null,h, w); cgpathaddlinetopoint(leftmarker,null,0, w); cgpathaddlinetopoint(leftmarker,null, 0, 0); cgcontextaddpath(context, leftmarker); cgcontextdrawpath(context, kcgpathfill); const cgaffinetransform rightmarkertransform=cgaffinetransformmakerotatetranslate(degrees_to_radians(90),self.frame.size.width,0); cgpathref rightmarker=cgpathcreatecopybytransformingpath(path, &rightmarkertransform); cgcontextaddpath(context, rightmarker); cgcontextdrawpath(context, kcgpathfill); const cgaffinetransform leftmarkerbottomtransform=cgaffinetransformmakerotatetranslate(degrees_to_radians(270),0,self.frame.size.height); cgpathref leftmarkerbottom=cgpathcreatecopybytransformingpath(path, &leftmarkerbottomtransform); cgcontextaddpath(context, leftmarkerbottom); cgcontextdrawpath(context, kcgpathfill); const cgaffinetransform rightmarkerbottomtransform=cgaffinetransformmakerotatetranslate(degrees_to_radians(180),self.frame.size.width,self.frame.size.height); cgpathref rightmarkerbottom=cgpathcreatecopybytransformingpath(path, &rightmarkerbottomtransform); cgcontextaddpath(context, rightmarkerbottom); cgcontextdrawpath(context, kcgpathfill); cgpathrelease(rightmarker); cgpathrelease(leftmarkerbottom); cgpathrelease(rightmarkerbottom); cgpathrelease(leftmarker); }
the pinch zoom code listed below
cgfloat lastscale; -(void) handlepinchgesture:(uipinchgesturerecognizer*)gesture{ uiview *gesturev=gesture.view; cgfloat scale=gesture.scale; switch (gesture.state) { case uigesturerecognizerstatebegan: if(lastscale<1.0){ lastscale=1.0; } scale=lastscale; break; default: break; } if(scale<1.0){ scale=1.0; } lastscale=scale; gesturev.transform=cgaffinetransformmakescale(scale, scale); //even not work ….[gesturev setneedsdisplay]; gesture.scale=scale; }
make sure have set (but should defaulted yes).
self.clearscontextbeforedrawing = yes;
from apple's docs in uiview
when set yes, drawing buffer automatically cleared transparent black before drawrect: method called. behavior ensures there no visual artifacts left on when view’s contents redrawn. if view’s opaque property set yes, backgroundcolor property of view must not nil or drawing errors may occur. default value of property yes.
if set value of property no, responsible ensuring contents of view drawn in drawrect: method. if drawing code heavily optimized, setting property no can improve performance, during scrolling when portion of view might need redrawn.
Comments
Post a Comment