2020-09-25

随笔

uiview 绘制圆角

方法一

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

   UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];

    shapeLayer.path= path.CGPath;

    self.layer.mask= shapeLayer;


方法二

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];

    CAShapeLayer*maskLayer = [[CAShapeLayeralloc]init];

    maskLayer.frame=self.bounds;

    maskLayer.path= maskPath.CGPath;

    self.layer.mask= maskLayer;


方法三

    CAShapeLayer *maskLayer = [CAShapeLayer layer];

    maskLayer.frame=self.bounds;


    CAShapeLayer*borderLayer = [CAShapeLayerlayer];

    borderLayer.frame=self.bounds;

    borderLayer.lineWidth= borderWidth;

    borderLayer.strokeColor= borderColor.CGColor;

    borderLayer.fillColor = [UIColor clearColor].CGColor;


    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:cornerRadius];

    maskLayer.path= bezierPath.CGPath;

    borderLayer.path= bezierPath.CGPath;

    [self.layer insertSublayer:borderLayer atIndex:0];

    [self.layersetMask:maskLayer];


方法四

    CAShapeLayer *layer = [[CAShapeLayer alloc] init];

    CGMutablePathRef pathRef = CGPathCreateMutable();

    CGRectbounds =CGRectInset(self.frame,0,0);

    CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));

    CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), cornerRadius);

    CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMinY(bounds), cornerRadius);

    CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMinX(bounds), CGRectGetMinY(bounds), cornerRadius);

    CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMinX(bounds), CGRectGetMaxY(bounds), cornerRadius);

    layer.path= pathRef;

    CFRelease(pathRef);

    layer.strokeColor= [borderColorCGColor];

    layer.fillColor= backgroudColor.CGColor;

    UIView*roundView = [[UIViewalloc]initWithFrame:bounds];

    [roundView.layerinsertSublayer:layeratIndex:0];

    roundView.backgroundColor = UIColor.clearColor;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。