#import "LXBezierPathView.h"
@interface LXBezierPathView()
@property(nonatomic,strong)UILabel *lable;
@end
@implementation LXBezierPathView
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.lable =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
self.lable.text = @"哈哈哈";
self.lable.textAlignment = NSTextAlignmentCenter;
self.lable.center = CGPointMake((frame.size.width )/2, (frame.size.width)/2);
[self addSubview:self.lable];
}
return self;
}
-(void)drawRect:(CGRect)rect{
CAShapeLayer *shapeLayer =[[CAShapeLayer alloc]init];
shapeLayer.frame = CGRectMake(0, 0, rect.size.width, rect.size.width);
shapeLayer.lineWidth = 10;
shapeLayer.fillColor =[UIColor clearColor].CGColor;
shapeLayer.strokeColor =[UIColor redColor].CGColor;
CGPoint center = CGPointMake((rect.size.width )/2, (rect.size.width)/2);
UIBezierPath *bezierPath =[UIBezierPath bezierPathWithArcCenter:center radius:(rect.size.width )/2 startAngle:-0.5 *M_PI endAngle:0.5 *M_PI clockwise:YES];
shapeLayer.path = bezierPath.CGPath;
[self.layer insertSublayer:shapeLayer above:shapeLayer];
}
我们绘制圆弧的时候,会设置贝塞尔曲线的半径为view的一半, 可是真正显示出来,会发现多了一点。这一点的长度刚好是线宽的一般。
所以需要处理的是
UIBezierPath *bezierPath =[UIBezierPath bezierPathWithArcCenter:center radius:(rect.size.width - 线宽)/2 startAngle:-0.5 *M_PI endAngle:0.5 *M_PI clockwise:YES];