iOS 关乎绘制圆弧的一个细节

1.png
#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];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容