概述
UIBezierPath用于定義一個由直線/曲線組合而成的路徑, 并且可以在自定義視圖中渲染該路徑. 在使用的過程中, 我們只需要先指定好路徑的結構, 比如一條直線越驻、一條貝塞爾曲線汁政、一個矩形、一個橢圓缀旁、一個圓弧等, 然后使用系統(tǒng)為我們提供的方法將構建好的路徑渲染出來即可记劈。UIBezierPath位于UIKit庫中, 是針對Core Graphics庫中的CGPathRef的封裝
1.基本畫線
// 創(chuàng)建path
UIBezierPath *path = [UIBezierPath bezierPath];
// 添加路徑1條點(100,100)到點(200,100)的線段]到path
[path moveToPoint:CGPointMake(100 , 100)];
[path addLineToPoint:CGPointMake(200, 100)];
// 將path繪制出來
[path stroke];
2.矩形
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(100, 10, 100, 100)];//矩形
path.lineWidth = 5;//邊框?qū)挾?/p>
path.lineCapStyle = kCGLineCapRound;//線條拐角樣式
path.lineJoinStyle = kCGLineCapRound;//終點拐角樣式
[[UIColor redColor] set];//設置填充顏色
[[UIColor orangeColor] setStroke];//邊框線條色
[path closePath];//關閉路徑
[path stroke];//渲染 繪制
[path fill];//填充顏色 不會遮擋邊框色
3.圓
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 10, 100, 100)];//圓
//UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 10, 100, 50)];//橢圓
path.lineWidth = 5;//邊框?qū)挾?/p>
[[UIColor redColor] set];//設置填充顏色
[[UIColor orangeColor] setStroke];//邊框線條色
[path closePath];//關閉路徑
[path stroke];//渲染 繪制
[path fill];//填充顏色 不會遮擋邊框色
4.圓弧
#define DEGREES_TO_RADIANS(degrees) ((degrees)*M_PI)/180
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width/2, self.frame.size.height/2) radius:50 startAngle:0 endAngle:DEGREES_TO_RADIANS(120) clockwise:YES];//clockwise YES順時針 NO逆時針
path.lineWidth = 2;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineCapRound;
[[UIColor redColor] setFill];
[path stroke];
5.貝塞爾曲線
// 第一種
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 2;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineCapRound;
[path moveToPoint:CGPointMake(100, 100)];
[path addQuadCurveToPoint:CGPointMake(250, 100) controlPoint:CGPointMake(150, 10)];
[[UIColor redColor] setStroke];
[path stroke];
//第二種
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineWidth = 2;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineCapRound;
[path moveToPoint:CGPointMake(100, 100)];
[path addCurveToPoint:CGPointMake(250, 100) controlPoint1:CGPointMake(150, 0) controlPoint2:CGPointMake(180, 120)];
[[UIColor redColor] setStroke];
[path stroke];
實例:
UIBezierPath *path = [UIBezierPath bezierPath];
CGSize size = self.bounds.size;
CGFloat margin = 10;
CGFloat radius = rintf(MIN(size.height - margin, size.width - margin)/4);
CGFloat xOffset, yOffset;
CGFloat offset = rintf((size.height - size.width) / 2);
if (offset > 0) {
xOffset = (CGFloat)rintf(margin/2);
yOffset = offset;
}else{
xOffset = -offset;
yOffset = rintf(margin/2);
}
[[UIColor redColor] setFill];
[path addArcWithCenter:CGPointMake(radius * 2 + xOffset, radius + yOffset)
radius:radius
startAngle:(CGFloat)-M_PI
endAngle:0
clockwise:YES];
[path addArcWithCenter:CGPointMake(radius * 3 + xOffset, radius * 2 + yOffset)
radius:radius
startAngle:(CGFloat)-M_PI_2
endAngle:(CGFloat)M_PI_2
clockwise:YES];
[path addArcWithCenter:CGPointMake(radius * 2 + xOffset, radius * 3 + yOffset)
radius:radius
startAngle:0
endAngle:(CGFloat)M_PI
clockwise:YES];
[path addArcWithCenter:CGPointMake(radius + xOffset, radius * 2 + yOffset)
radius:radius
startAngle:(CGFloat)M_PI_2
endAngle:(CGFloat)-M_PI_2
clockwise:YES];
[path closePath];
[path fill];
6.CAShapeLayer與UIBezierPath
CGPoint point1 = CGPointMake(50, 10);
CGPoint point2 = CGPointMake(200, 10);
CGPoint point3 = CGPointMake(200, 40);
CGPoint point4 = CGPointMake(230, 40);
CGPoint point5 = CGPointMake(200, 60);
CGPoint point6 = CGPointMake(200, 150);
CGPoint point7 = CGPointMake(50, 150);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:point1];
[path addLineToPoint:point2];
[path addLineToPoint:point3];
[path addLineToPoint:point4];
[path addLineToPoint:point5];
[path addLineToPoint:point6];
[path addLineToPoint:point7];
[path closePath];
CAShapeLayer *shapelayer = [CAShapeLayer layer];
shapelayer.path = path.CGPath;
shapelayer.fillColor = [[UIColor orangeColor] CGColor];
[self.layer addSublayer:shapelayer];