iOS UIBezierPath類 介紹

使用UIBezierPath類可以創建基于矢量的路徑,這個類在UIKit中。此類是Core Graphics框架關于path的一個封裝。使用此類可以定義簡單的形狀,如橢圓或者矩形,或者有多個直線和曲線段組成的形狀。

Bezier Path 基礎

  • 使用UIBezierPath創建多邊形
QQ20160428-0.png

UIColor *color = [UIColor yellowColor];
[color set]; //設置線條顏色
UIBezierPath *aPath = [UIBezierPath bezierPath];
aPath.lineWidth = 2.0; //設置線寬
aPath.lineCapStyle = kCGLineCapRound; //線條拐角
aPath.lineJoinStyle = kCGLineCapRound; //終點處理
[aPath moveToPoint:CGPointMake(100, 20)];
[aPath addLineToPoint:CGPointMake(150, 50)];
[aPath addLineToPoint:CGPointMake(50, 50)];
[aPath closePath];
[aPath stroke];//根據坐標點連線 ([aPath fill];填充)

  • 使用UIBezierPath創建矩形
QQ20160428-0.png

UIColor color = [UIColor yellowColor];
[color set];
UIBezierPath
aPath = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20,100, 50)];
aPath.lineWidth = 2.0;
aPath.lineCapStyle = kCGLineCapRound;
aPath.lineJoinStyle = kCGLineCapRound;
[aPath stroke];

  • 使用UIBezierPath創建圓形或者橢圓形
QQ20160428-0.png
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
當傳入的rect是一個正方形時,繪制的圖像是一個內切圓;當傳入的rect是一個長方形時,繪制的圖像是一個內切橢圓。

UIColor color = [UIColor yellowColor];
[color set]; //設置線條顏色
UIBezierPath
aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, 100, 50)];
aPath.lineWidth = 2.0;
aPath.lineCapStyle = kCGLineCapRound; //線條拐角
aPath.lineJoinStyle = kCGLineCapRound; //終點處理
[aPath stroke];

  • 使用UIBezierPath創建一段弧線
QQ20160428-0.png
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
center圓弧的中心點、radius半徑、startAngle開始角度、endAngle結束角度、clockwise是否順時針方向。

UIColor ***color = [UIColor yellowColor];
[color set];
UIBezierPath *aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:radiuse startAngle:0 endAngle:radians(50) clockwise:YES];
aPath.lineWidth = 2.0;
aPath.lineCapStyle = kCGLineCapRound;
aPath.lineJoinStyle = kCGLineCapRound;
[aPath stroke];

  • UIBezierPath虛線
QQ20160428-1.png

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(20, 100)];
[path addLineToPoint:CGPointMake(20, 20)];
CGFloat dash[] = {2,2};
[path setLineDash:dash count:2 phase:0];//!!!
[[UIColor yellowColor] setStroke];
[path stroke];

  • UIBezierPath還提供了貝塞爾曲線
QQ20160428-0.png

UIColor color = [UIColor yellowColor];
[color set];
UIBezierPath
aPath = [UIBezierPath bezierPath];
aPath.lineWidth = 1.0;
aPath.lineCapStyle = kCGLineCapRound;
aPath.lineJoinStyle = kCGLineCapRound;
[aPath moveToPoint:CGPointMake(20, 100)];
[aPath addQuadCurveToPoint:CGPointMake(120, 100) controlPoint:CGPointMake(10, 20)];
[aPath stroke];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容