How does iOS draw dashed lines?

Source: Internet
Author: User

Overriding the DrawRect method

Preparing: Inheriting UIView subclasses

1.. h file

//

DashesLineView.h

Inface

//

Created by Huangzengsong on 15/5/11.

Copyright (c) 2015 Huangzs. All rights reserved.

//

#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

2.. m file

//

dasheslineview.m

Inface

//

Created by Huangzengsong on 15/5/11.

Copyright (c) 2015 Huangzs. All rights reserved.

//

#import "DashesLineView.h"

@implementation Dasheslineview

-(ID) initWithFrame: (CGRect) frame

{

self= [Super Initwithframe:frame];

if (self) {

Initialization code

}

return self;

}

Only override Drawrect:if perform custom drawing.

An empty implementation adversely affects performance during animation.

-(void) DrawRect: (cgrect) rect

{

Cgcontextref context =uigraphicsgetcurrentcontext ();

Cgcontextbeginpath (context);

Cgcontextsetlinewidth (context,1);//Line width

Cgcontextsetstrokecolorwithcolor (Context,self.linecolor.cgcolor);

CGFloat lengths[] = {1,2};//First draw 4 points and then draw 2 points

Cgcontextsetlinedash (context,0, lengths,2);//Note the value of 2 (count) equals 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);

}

/*

Only override Drawrect:if perform custom drawing.

An empty implementation adversely affects performance during animation.

-(void) DrawRect: (cgrect) Rect {

Drawing Code

}

*/

@end

3. When to adjust

Self. Dashlineview.startpoint=cgpointmake (0, 1);

Self. Dashlineview.endpoint=cgpointmake (

[[UIScreen Mainscreen] Bounds].size.width

-40, 1);

Self. Dashlineview.linecolor=

[Uicolor colorwithred:28.0/255.0 green:160.0/255.0 blue:229.0/255.0 alpha:1]

;

How does iOS draw dashed lines?

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.