ios - Animation when drawing with CGContext -
my question how animate drawing process when drawing cgcontextref
. possible? assuming is, how?
i have 2 code snippets animate. first 1 draws progress bar , second 1 draws simple line chart. drawing done inside subclass of uiview
.
progress bar nice , easy. want sort of draw out left. pretty sure require using other uirectfill
dont know how accomplish it.
- (void)drawprogressline { [bgcolor set]; uirectfill(self.bounds); [graphcolor set]; uirectfill(cgrectmake(0, 0, self.frame.size.width / 100 * [[items objectatindex:0] floatvalue], self.frame.size.height)); }
the line chart bit more complex. really start drawing left line line completing towards right if how can fade in? code:
- (void)drawlinechart { [bgcolor set]; uirectfill(self.bounds); [graphcolor set]; if (items.count < 2) return; cgrect bounds = cgrectmake(0, 50, self.bounds.size.width, self.bounds.size.height - 100); float max = -1; (graphitem *item in items) if (item.value > max) max = item.value; float xstep = (self.frame.size.width) / (items.count - 1); (int = 0; < items.count; i++) { if (i == items.count - 1) break; float itemheight = bounds.origin.y + bounds.size.height - ((graphitem*)[items objectatindex:i]).value / max * bounds.size.height; float nextitemheight = bounds.origin.y + bounds.size.height - ((graphitem*)[items objectatindex:i + 1]).value / max * bounds.size.height; cgpoint start = cgpointmake(xstep * i, itemheight); cgpoint stop = cgpointmake(xstep * (i + 1), nextitemheight); [self drawlinefrompoint:start topoint:stop linewidth:1 color:graphcolor shadow:yes]; } }
pretty simple guess. if important drawlinefrompoint.....
implemented like:
- (void)drawlinefrompoint:(cgpoint)startpoint topoint:(cgpoint)endpoint linewidth:(cgfloat)width color:(uicolor *)color shadow:(bool)shadow { if (shadow) { cgcolorspaceref colorspace = cgcolorspacecreatedevicergb(); cgfloat components[4] = {0.0, 0.0, 0.0, 1.0}; cgcolorref shadowcolor = cgcolorcreate(colorspace, components); cgcontextsetshadowwithcolor(uigraphicsgetcurrentcontext(), cgsizemake(1,1), 2.0, shadowcolor); } cgcontextbeginpath(context); cgcontextsetlinewidth(context, width); cgcontextmovetopoint(context, startpoint.x, startpoint.y); cgcontextaddlinetopoint(context, endpoint.x, endpoint.y); cgcontextclosepath(context); [color setstroke]; cgcontextstrokepath(context); cgcontextsetshadowwithcolor(context, cgsizezero, 0, null); }
i hope made myself clear cause 1 in country , post last thing stands between me , bed. cheers, jan.
it sounds don't understand uikit view drawing cycle. understand each time want change appearance of custom-drawn view, need send setneedsdisplay
? , need redraw entirely in drawrect:
method? drawing doesn't appear on screen until drawrect:
returns, , after cannot draw more in view until receives drawrect:
message. if want contents of view animated, need send setneedsdisplay
view periodically (say, every 1/30th or 1/60th of second, using either nstimer
or cadisplaylink
).
Comments
Post a Comment