1、画三角形
效果图
实现:FBDrawView在继承的View类中的- (void)drawRect:(CGRect)rect方法中调用
例如:
#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];
}
代码函数 :
#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;
//设置填充颜色
UIColor*fillcolor=[UIColorredColor];
[fillcolorset];
[pathfill];
//设置画笔颜色
UIColor*strokeColo=[UIColorblueColor];
[strokeColoset];
[pathstroke];
}
虚线效果图
函数代码:
#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是数据值count是数据个数phase是从第几个值开始
[pathsetLineDash:dashcount:2phase:0];
//
//设置填充颜色
UIColor*fillcolor=[UIColorredColor];
[fillcolorset];
[pathfill];
//设置画笔颜色
UIColor*strokeColo=[UIColorblueColor];
[strokeColoset];
[pathstroke];
}