ios - how to create Start and end point straight line using objective c -
can please tell me, how add starting point end point straight line. , have added button drag same line tough end point call method. please tell me need code.
there 2 common techniques.
using cashapelayer:
create uibezierpath (replace coordinates whatever want):
uibezierpath *path = [uibezierpath bezierpath]; [path movetopoint:cgpointmake(10.0, 10.0)]; [path addlinetopoint:cgpointmake(100.0, 100.0)];
create cashapelayer uses uibezierpath:
cashapelayer *shapelayer = [cashapelayer layer]; shapelayer.path = [path cgpath]; shapelayer.strokecolor = [[uicolor bluecolor] cgcolor]; shapelayer.linewidth = 3.0; shapelayer.fillcolor = [[uicolor clearcolor] cgcolor];
add cashapelayer view's layer:
[self.view.layer addsublayer:shapelayer];
in previous versions of xcode, had manually add quartzcore.framework project's "link binary libraries" , import header in .m file, that's not necessary anymore (if have "enable modules" , "link frameworks automatically" build settings turned on).
the other approach subclass uiview , use coregraphics calls in drawrect method:
create uiview subclass , define drawrect draws line:
- (void)drawrect:(cgrect)rect { cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextsetstrokecolorwithcolor(context, [[uicolor bluecolor] cgcolor]); cgcontextsetlinewidth(context, 3.0); cgcontextmovetopoint(context, 10.0, 10.0); cgcontextaddlinetopoint(context, 100.0, 100.0); cgcontextdrawpath(context, kcgpathstroke); }
then can either use view class base class nib/storyboard or view, or can have view controller programmatically add subview:
customview *view = [[customview alloc] initwithframe:self.view.bounds]; view.backgroundcolor = [uicolor clearcolor]; [self.view addsubview:view];
Comments
Post a Comment