快速上手IOS UIBezierPath(贝塞尔曲线)

2020-01-18 21:43:05王旭

添加圆弧


 UIBezierPath *path = [UIBezierPath bezierPath];
 [path moveToPoint:CGPointMake(200, 400)];
 [path addLineToPoint:CGPointMake(225, 410)];
 [path addArcWithCenter:CGPointMake(200, 400) radius:25 startAngle:0 endAngle:1.5*M_PI clockwise:YES];
// [path closePath];
// [path removeAllPoints];
 [path stroke];

uibezierpath,ios,贝塞尔曲线,贝塞尔曲线案例

追加路径


UIBezierPath *path_A = [UIBezierPath bezierPath];
[path_A moveToPoint:CGPointMake(200, 500)];
[path_A addLineToPoint:CGPointMake(225, 410)];
UIBezierPath *path_B = [UIBezierPath bezierPath];
[path_B moveToPoint:CGPointMake(200, 600)];
[path_B addLineToPoint:CGPointMake(225, 500)];
[path_A appendPath:path_B];
[path_A stroke];

uibezierpath,ios,贝塞尔曲线,贝塞尔曲线案例

创建翻转路径,即起点变成终点,终点变成起点


 UIBezierPath *path = [UIBezierPath bezierPath];
 [path moveToPoint:CGPointMake(50, 50)];
 [path addLineToPoint:CGPointMake(100, 50)];
 path.lineWidth = 5.0f;
 NSLog(@"%@",NSStringFromCGPoint(path.currentPoint));
 UIBezierPath *path_b = [path bezierPathByReversingPath];
 CGAffineTransform transform = CGAffineTransformMakeTranslation(200, 0);
 [path_b applyTransform: transform];
 // 两条路径分别添加一条直接到 self.center
 [path addLineToPoint: CGPointMake(self.frame.size.width*0.5, self.frame.size.height*0.5)];
 [path_b addLineToPoint: CGPointMake(self.frame.size.width*0.5, self.frame.size.height*0.5)];
 NSLog(@"%@",NSStringFromCGPoint(path_b.currentPoint));
 [[UIColor redColor] set];
 [path stroke];
 [[UIColor blueColor] set];
 [path_b stroke];

uibezierpath,ios,贝塞尔曲线,贝塞尔曲线案例

路径进行仿射变换


 UIBezierPath *path = [UIBezierPath bezierPath];
 [path moveToPoint:CGPointMake(100, 50)];
 [path addLineToPoint:CGPointMake(200, 50)];
 CGAffineTransform transform = CGAffineTransformRotate(self.transform, M_PI_4);
 [path applyTransform:transform];
 path.lineWidth = 5.0f;
 [path stroke];

uibezierpath,ios,贝塞尔曲线,贝塞尔曲线案例

创建虚线


CGFloat dashStyle[] = {1.0f, 2.0f};
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(50, 50)];
[path addLineToPoint:CGPointMake(100, 50)];
[path setLineDash:dashStyle count:2 phase:0.0];
[path stroke];