1、畫三角形
效果圖
實(shí)現(xiàn):FBDrawView在繼承的View類中的- (void)drawRect:(CGRect)rect方法中調(diào)用
例如:
#import"FBDrawView.h"
@implementationFBDrawView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
*/
- (void)drawRect:(CGRect)rect {
// Drawing code
[selfdrawTrianglePath];
}
代碼函數(shù) :
#pragma畫三角形
-(void)drawTrianglePath{
UIBezierPath*path=[UIBezierPathbezierPath];
[pathmoveToPoint:CGPointMake(20,20)];
[pathaddLineToPoint:CGPointMake(self.frame.size.width-40,20)];
[pathaddLineToPoint:CGPointMake(self.frame.size.width/2,self.frame.size.height)];
[pathclosePath];//閉合
path.lineWidth=1.5;
//設(shè)置填充顏色
UIColor*fillcolor=[UIColorredColor];
[fillcolorset];
[pathfill];
//設(shè)置畫筆顏色
UIColor*strokeColo=[UIColorblueColor];
[strokeColoset];
[pathstroke];
}
虛線效果圖
? ?
函數(shù)代碼:
#pragma畫三角形
-(void)drawTrianglePath{
UIBezierPath*path=[UIBezierPathbezierPath];
[pathmoveToPoint:CGPointMake(20,20)];
[pathaddLineToPoint:CGPointMake(self.frame.size.width-40,20)];
[pathaddLineToPoint:CGPointMake(self.frame.size.width/2,self.frame.size.height)];
[pathclosePath];//閉合
path.lineWidth=8;
//邊框是虛線
CGFloatdash[]={20,10};
//dash是數(shù)據(jù)值count是數(shù)據(jù)個(gè)數(shù)phase是從第幾個(gè)值開始
[pathsetLineDash:dashcount:2phase:0];
//
//設(shè)置填充顏色
UIColor*fillcolor=[UIColorredColor];
[fillcolorset];
[pathfill];
//設(shè)置畫筆顏色
UIColor*strokeColo=[UIColorblueColor];
[strokeColoset];
[pathstroke];
}