A summary of how IOS draws dashes _ios

Source: Internet
Author: User

First, rewrite the DrawRect method.

-(void) DrawRect: (cgrect) rect
{
 [super Drawrect:rect];
Cgcontextref CurrentContext = Uigraphicsgetcurrentcontext ();
Sets the dashed color
 cgcontextsetstrokecolorwithcolor (currentcontext, [Uicolor Blackcolor]. Cgcolor);
 Set dashed width
 cgcontextsetlinewidth (currentcontext, 1);
 Sets the dotted line
 to draw the starting point Cgcontextmovetopoint (currentcontext, 0, 0);
 Sets the dotted line
 to draw the endpoint Cgcontextaddlinetopoint (currentcontext, self.frame.origin.x + self.frame.size.width, 0);
 Set the width interval for dotted lines: the numbers in the arr below indicate that 3 points are drawn first and then a point
 cgfloat arr[] = {3,1};
 The last argument "2" below represents the number of permutations.
 Cgcontextsetlinedash (currentcontext, 0, arr, 2);
 Cgcontextdrawpath (CurrentContext, Kcgpathstroke);
 
}

Second, the use of Cashapelayer way to draw a dashed line

Cashapelayer *shapelayer = [Cashapelayer layer];
[Shapelayer setBounds:self.bounds];
[Shapelayer Setposition:cgpointmake (self.frame.size.width/2.0, Self.frame.size.height)];
[Shapelayer Setfillcolor:[uicolor Clearcolor]. Cgcolor];
Sets the dashed color
shapelayer setstrokecolor:[uicolor Blackcolor]. Cgcolor];
Set dashed width
[shapelayer setLineWidth:self.frame.size.height];
[Shapelayer Setlinejoin:kcalinejoinround];
Sets the line width and spacing of dashes
 [Shapelayer setlinedashpattern:[nsarray arraywithobjects:[nsnumber numberwithint:3], [NSNumber Numberwithint:1], nil]];
 Create dotted line drawing path
 cgmutablepathref path = cgpathcreatemutable ();
 Sets the dotted line drawing path start
 cgpathmovetopoint (path, NULL, 0, 0);
 Sets the dotted line drawing path endpoint
 cgpathaddlinetopoint (path, NULL, Self.frame.size.width, 0);
 Set dashed line drawing path
 [Shapelayer Setpath:path];
 Cgpathrelease (path);
 Add dotted line
 [Self.layer Addsublayer:shapelayer];

About this way already someone has sorted out a very useful class method, see the following code, note: The following is not complete code, if necessary, please own Baidu search.

/**
 * * Lineview:  View * * linelength that needs to be drawn into a dashed line
 : The  width of the dashed line
 * * linespacing: Dotted spacing
 * * LineColor:  Dashed color
 **/
 
+ (void) Drawdashline: (UIView *) Lineview linelength: (int) linelength linespacing: (int) linespacing LineColor: (Uicolor *) linecolor
{
 Cashapelayer *shapelayer = [Cashapelayer layer];
 .....
 [Shapelayer SetStrokeColor:lineColor.CGColor];
 ......
 [Shapelayer Setlinedashpattern:[nsarray Arraywithobjects:[nsnumber numberwithint:linelength], [NSNumber Numberwithint:linespacing], nil]];
 ......
 [Lineview.layer Addsublayer:shapelayer];
 
}

Third, the economical type: uses the mapping way to draw the dash line (needs the designer Chettu to cooperate)

Uiimageview *imgdashlineview =[[uiimageview alloc] Initwithframe:cgrectmake (self.view.frame.size.width-30, 1 )];

[Imgdashlineview setbackgroundcolor:[uicolor colorwithpatternimage:[uiimage imagenamed:@ "Xuxian.png"]];

[Self.view Addsubview:imgdashlineview];

Summarize

The above content is from the network, in the spirit of sharing learning, if there are infringement issues, please inform. The above is the entire content of this article, welcome everyone to discuss learning, there are questions please leave a message, small series will be as soon as possible to answer your question.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.