定义
一个包含直线和曲线段的并且可以在自定义view上展示的路径。
方法:
1、返回一个矩形曲线
+ (instancetype)bezierPathWithRect:(CGRect)rect;
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(100,100,200,200)];
CAShapeLayer *shape = [CAShapeLayer layer];
shape.path= bezierPath.CGPath;
shape.frame=CGRectMake(0,0,500,200);
shape.fillColor = [UIColor whiteColor].CGColor;
shape.strokeColor = [UIColor blackColor].CGColor;
shape.backgroundColor = [UIColor yellowColor].CGColor;
[self.view.layeraddSublayer:shape];

2、返回一个椭圆
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50,100,300,200)];

3、返回一个带圆角的矩形,四个圆角都一样
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;// rounds all corners with the same horizontal and vertical radius

4、返回一个带圆角的矩形,可以指定那几个角带圆角
corners:圆角(左上、左下、右上、右下)
cornerRadii:圆角大小,虽然是个size,但是如果两个值不一样的话,以第一个为准
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50,100,300,200) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(50,10)];

5、 返回一个圆上的弧
center:圆的中心点
radius:圆的半径
startAngle:开始角度
endAngle:结束角度
clockwise:yes:顺时针 no:逆时针
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(200, 200) radius:50 startAngle:0 endAngle:3.14 clockwise:NO];

6、返回一个曲线 这个曲线根据两个点和一个控制点来控制弧度,这是一个由一个控制点控制的二次曲线
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
endPoint:结束点
controlPoint:控制点
还有一个隐藏的参数:currentpoint

[bezierPath addQuadCurveToPoint:CGPointMake(300,100) controlPoint:CGPointMake(280,150)];

7、返回一个由两个控制点控制的三次曲线,
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
endPoint:结束点
controlPoint1:控制点一
controlPoint2:控制点二
ex:

[bezierPathaddCurveToPoint:CGPointMake(200, 100) controlPoint1:CGPointMake(100, 50) controlPoint2:CGPointMake(100, 200)];

8、关闭bezier曲线
在曲线的firstpoint和endpoint之间创建一个线段关闭贝塞尔曲线,并随后把currentpoint更新为新创建的线段的endpoint(也就是贝塞尔曲线的firstpoint),特别注意这个点
[bezierPath closePath];

这时候bezierPath 的currentpoint 会变成a点,而不是b点。
9、创建一个相同的path的曲线,形状一样,但是绘制方向相反,可以打印bezierPath绘制后的crrentpoint和resverpath绘制后的crrentpoint,第一个是{400,99.9},第二个是{20,100}。所以经过反转后的current point是第一个path的beginpoint
ex:UIBezierPath*resverpath = [bezierPathbezierPathByReversingPath];

网友评论