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];
}