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?