- (void) drawline{Cashapelayer*shapelayer =[Cashapelayer layer]; [Shapelayer SetBounds:self.bounds]; [Shapelayer SetPosition:self.center]; [Shapelayer Setfillcolor:[[uicolor Clearcolor] cgcolor]; //set the dashed line color to Blackcolor [Shapelayer Setstrokecolor:[[uicolor Blackcolor] cgcolor]];[Shapelayer setstrokecolor:[[uicolor colorwithred:223/255.0Green223/255.0Blue223/255.0Alpha1.0f] [Cgcolor]]; //3.0f Set the width of the dashed line[Shapelayer setlinewidth:1.0f]; [Shapelayer Setlinejoin:kcalinejoinround]; //3 = width of the line 1 = spacing per line[Shapelayer setlinedashpattern:[nsarray arraywithobjects:[nsnumber Numberwithint:1],[nsnumber Numberwithint:1],nil]]; //Setup the pathCgmutablepathref Path =cgpathcreatemutable (); //0, 10 represents the X, y of the initial coordinate//320, 10 represents the X, y of the initial coordinateCgpathmovetopoint (Path, NULL,0,0); Cgpathaddlinetopoint (Path, NULL,0, the); [Shapelayer Setpath:path]; Cgpathrelease (path); [_verticalline.layer Addsublayer:shapelayer];}
Or
#import <UIKit/UIKit.h>@interface linedashview:uiview@property (nonatomic, strong) Nsarray * Linedashpattern; // Segment Segmentation mode @property (nonatomic, assign) cgfloat Endoffset; // value between 0.001 and 0.499 -(Instancetype) initWithFrame: (CGRect) frame linedashpattern: (Nsarray *) Linedashpattern Endoffset: (cgfloat) endoffset; @end
#import "LineDashView.h"@interfaceLinedashview () {Cashapelayer*_shapelayer;}@end@implementationLinedashview- (ID) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {Uibezierpath*path =[Uibezierpath bezierPathWithRect:self.bounds]; _shapelayer= (Cashapelayer *) Self.layer; _shapelayer.fillcolor=[Uicolor Clearcolor]. Cgcolor; _shapelayer.strokestart=0.001; _shapelayer.strokeend=0.499; _shapelayer.linewidth=Frame.size.width; _shapelayer.path=path. Cgpath; } returnSelf ;}-(Instancetype) initWithFrame: (CGRect) frame linedashpattern: (Nsarray*) Linedashpattern Endoffset: (cgfloat) endoffset{Linedashview*linedashview =[[Linedashview alloc] initwithframe:frame]; Linedashview.linedashpattern=Linedashpattern; Linedashview.endoffset=Endoffset; returnLinedashview;}#pragmaMark-Modify View's Backedlayer to cashapelayer+(Class) layerclass{return[Cashapelayerclass];}#pragmaMark-Getter,setter method for overriding attributes@synthesizeLinedashpattern =_linedashpattern;- (void) Setlinedashpattern: (Nsarray *) linedashpattern{_linedashpattern=Linedashpattern; _shapelayer.linedashpattern=Linedashpattern;}-(Nsarray *) linedashpattern{return_linedashpattern;}@synthesizeEndoffset =_endoffset;- (void) Setendoffset: (cgfloat) endoffset{_endoffset=Endoffset; if(Endoffset <0.499&& Endoffset >0.001) {_shapelayer.strokeend=_endoffset; }}-(cgfloat) endoffset{return_endoffset;}#pragmaMark-Rewritten the system's BackgroundColor property-(void) SetBackgroundColor: (Uicolor *) backgroundcolor{_shapelayer.strokecolor=Backgroundcolor.cgcolor;}-(Uicolor *) backgroundcolor{return[Uicolor Colorwithcgcolor:_shapelayer.strokecolor];}@end
1 linedashpattern:@[@1, @1] Endoffset:0.495] ; = [Uicolor redcolor]; [Self.contentview addsubview:line1];
UIView Draw Dashed Line