来源 https://www.cnblogs.com/jaesun/p/iOS-CAShapeLayerUIBezierPath-hua-xian.html

+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise
center:圆弧的中心,相对所在视图; radius:圆弧半径; startAngle:起始点的角度(相对角度坐标系0); endAngle:结束点的角度(相对角度坐标系0); clockwise:是否为顺时针方向。
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise
在原有的线上添加一条弧线 。center:圆弧的中心,相对所在视图; radius:圆弧半径; startAngle:起始点的角度(相对角度坐标系0); endAngle:结束点的角度(相对角度坐标系0); clockwise:是否为顺时针方向。
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame) - 100, CGRectGetMidY(self.view.frame) - 100, 200, 200)];
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
// 线的路径 CGPoint viewCenter = CGPointMake(view.frame.size.width / 2.0, view.frame.size.height / 2.0); // 画弧的中心点,相对于view
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:viewCenter radius:50.0 startAngle:0 endAngle:M_PI_2 clockwise:YES];
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.lineWidth = 2;
pathLayer.strokeColor = [UIColor greenColor].CGColor;
pathLayer.fillColor = nil; // 默认为blackColor pathLayer.path = path.CGPath;
[view.layer addSublayer:pathLayer];

在矩形中画内切椭圆
// 需要圆视图 UIView * view = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame) - 130, CGRectGetMidY(self.view.frame) - 100, 260, 200)];
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
// 线的路径 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:view.bounds];
CAShapeLayer *pathLayer = [CAShapeLayer layer];
pathLayer.lineWidth = 2;
pathLayer.strokeColor = [UIColor greenColor].CGColor;
pathLayer.path = path.CGPath;
pathLayer.fillColor = nil; // 默认为blackColor [view.layer addSublayer:pathLayer];

网友评论