UIBezierPath類可以創建基於矢量的路徑,可以定義簡單的形狀,如橢圓或者矩形,或者有多個直線和曲線段組成的形狀。
一、UIBezierPath使用:
1、創建path;
2、添加路徑到path;
3、將path繪制出來;
1 //創建path 2 path = [UIBezierPath bezierPath]; 3 //添加路徑 4 [path moveToPoint:(CGPoint){10,50}]; 5 [path addLineToPoint:(CGPoint){100,50}]; 6 //將path繪制出來 7 [path stroke]; 8
二、實例
1、繪制多邊形
注意:這個類要繼承自UIView。
1 #import "Draw.h" 2 3 @interface Draw (){ 4 5 UIBezierPath *path; 6 7 } 8 9 @end 10 11 - (void)drawRect:(CGRect)rect { 12 13 //線條顏色 14 UIColor *color = [UIColor orangeColor]; 15 [color set]; 16 17 //創建path 18 path = [UIBezierPath bezierPath]; 19 //設置線寬 20 path.lineWidth = 3; 21 //線條拐角 22 path.lineCapStyle = kCGLineCapRound; 23 //終點處理 24 path.lineJoinStyle = kCGLineJoinRound; 25 26 [path moveToPoint:(CGPoint){100,100}]; 27 [path addLineToPoint:(CGPoint){200,100}]; 28 [path addLineToPoint:(CGPoint){250,150}]; 29 [path addLineToPoint:(CGPoint){200,200}]; 30 [path addLineToPoint:(CGPoint){100,200}]; 31 [path addLineToPoint:(CGPoint){50,150}]; 32 [path closePath]; 33 //根據坐標點連線 34 35 [path stroke]; 36 37 }
如果修改最后一句代碼將[path stroke]改成[path fill];
下面來看看區別,
2、繪制矩形
+ (UIBezierPath *)bezierPathWithRect:(CGRect)rect;
1 - (void)drawRect:(CGRect)rect { 2 3 //線條顏色 4 UIColor *color = [UIColor orangeColor]; 5 [color set]; 6 7 //創建path 8 //rect四個值分別為(x、y、矩形長,矩形寬) 9 path = [UIBezierPath bezierPathWithRect:(CGRect){10,20,100,50}]; 10 //設置線寬 11 path.lineWidth = 3; 12 //線條拐角 13 path.lineCapStyle = kCGLineCapRound; 14 //終點處理 15 path.lineJoinStyle = kCGLineJoinRound; 16 17 //根據坐標點連線 18 [path stroke]; 19 20 }
3、繪制圓形或橢圓形
繪制圓形或橢圓形,我們我用
+ (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect;
1 - (void)drawRect:(CGRect)rect { 2 3 //線條顏色 4 UIColor *color = [UIColor orangeColor]; 5 [color set]; 6 7 //添加路徑 8 path = [UIBezierPath bezierPathWithOvalInRect:(CGRect){50,50,100,100}]; 9 path.lineWidth = 3; 10 //線條拐角 11 path.lineCapStyle = kCGLineCapRound; 12 //終點處理 13 path.lineJoinStyle = kCGLineJoinRound; 14 //根據坐標點連線 15 [path stroke]; 16 17 }
下面改變rect值,
path = [UIBezierPath bezierPathWithOvalInRect:(CGRect){50,50,100,50}];
4、繪制弧線
繪制弧線用方法:
+ (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center
radius:(CGFloat)radius
startAngle:(CGFloat)startAngle
endAngle:(CGFloat)endAngle
clockwise:(BOOL)clockwise;
其中 Center:圓弧的中心;
radius:半徑;
startAngle:開始角度;
endAngle:結束角度;
clockwise:是否順時針方向;
#import "Draw.h" //定義PI值 #define PI 3.14159265359 @interface Draw (){ UIBezierPath *path; } - (void)drawRect:(CGRect)rect { //線條顏色 UIColor *color = [UIColor orangeColor]; [color set]; //添加路徑 path = [UIBezierPath bezierPathWithArcCenter:(CGPoint){100,50} radius:50 startAngle:0 endAngle:PI*0.5 clockwise:YES ]; path.lineWidth = 3; //線條拐角 path.lineCapStyle = kCGLineCapRound; //終點處理 path.lineJoinStyle = kCGLineJoinRound; //根據坐標點連線 [path stroke]; }
5、二次貝塞爾曲線和三次貝塞爾曲線的繪制
曲線段在當前點開始,在指定的點結束;曲線的形狀有開始點,結束點,一個或者多個控制點的切線定義。
下圖顯示了兩種曲線類型的相似,以及控制點和curve形狀的關系。
(1) 繪制二次貝塞爾曲線
方法:- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
1 - (void)drawRect:(CGRect)rect { 2 3 //線條顏色 4 UIColor *color = [UIColor orangeColor]; 5 [color set]; 6 7 //添加路徑 8 path = [UIBezierPath bezierPath]; 9 10 path.lineWidth = 3; 11 //線條拐角 12 path.lineCapStyle = kCGLineCapRound; 13 //終點處理 14 path.lineJoinStyle = kCGLineJoinRound; 15 16 [path moveToPoint:(CGPoint){20,100}]; 17 [path addQuadCurveToPoint:(CGPoint){100,100} controlPoint:(CGPoint){50,20}]; 18 19 //根據坐標點連線 20 [path stroke]; 21 22 }
(2) 繪制三次貝塞爾曲線
方法:- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
1 - (void)drawRect:(CGRect)rect { 2 3 //線條顏色 4 UIColor *color = [UIColor orangeColor]; 5 [color set]; 6 7 //添加路徑 8 path = [UIBezierPath bezierPath]; 9 10 path.lineWidth = 3; 11 //線條拐角 12 path.lineCapStyle = kCGLineCapRound; 13 //終點處理 14 path.lineJoinStyle = kCGLineJoinRound; 15 16 [path moveToPoint:(CGPoint){20,100}]; 17 [path addCurveToPoint:(CGPoint){150,70} controlPoint1:(CGPoint){70,30} controlPoint2:(CGPoint){80,120}]; 18 19 //根據坐標點連線 20 [path stroke]; 21 22 }