because the project needs to draw a dashed line, initially want to use the picture repeat overlay implementation. A lap or a uiview.
The code is as follows
. h file
#import <UIKit/UIKit.h> @interface dasheslineview:uiview@property (nonatomic) Cgpoint startpoint;// Dashed start @property (nonatomic) cgpoint endpoint;//dashed End @property (nonatomic,strong) uicolor* linecolor;//dashed color @end
. m file
#import "DashesLineView.h" #define KINTERVAL 10//Global pitch @implementation dasheslineview-(ID) i Nitwithframe: (cgrect) frame{self= [Super Initwithframe:frame]; if (self) {_linecolor = [uicolor Redcolor]; _startpoint = Cgpointmake (0, 1); _endpoint = Cgpointmake (screen_width, 1); } return self;} -(void) DrawRect: (cgrect) Rect {cgcontextref context = Uigraphicsgetcurrentcontext (); Cgcontextbeginpath (context); Cgcontextsetlinewidth (context,0.5);//Line width cgcontextsetstrokecolorwithcolor (context,self.linecolor.cgcolor); CGFloat lengths[] = {4,2};//First draw 4 points and then draw 2 points Cgcontextsetlinedash (context,0, lengths,2);//Note 2 (count) is equal to the length of the lengths array Cgcontextmovetopoint (CONTEXT,SELF.STARTPOINT.X,SELF.STARTPOINT.Y); Cgcontextaddlinetopoint (CONTEXT,SELF.ENDPOINT.X,SELF.ENDPOINT.Y); Cgcontextstrokepath (context); Cgcontextclosepath (context); } @end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
How IOS implements dashed line drawing