美文网首页
在view里画个空心圆,相当于遮罩出一个空心圆

在view里画个空心圆,相当于遮罩出一个空心圆

作者: 无名指的情怀 | 来源:发表于2019-12-20 16:34 被阅读0次

看效果

方法一:

代码如下,自定义UIView,重写以下方法

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

    //在view里画个空心圆,相当于遮罩出一个空心圆

    CGFloatwidth =self.bounds.size.width;

    CGFloatheight =self.bounds.size.height;

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.f, 0.f, width, height) cornerRadius:1];

    CGFloatX =20;

    CGFloatR =MIN((width-2*X)/2, (height-2*X)/2);

    UIBezierPath *path1 = [[UIBezierPath bezierPathWithRoundedRect:CGRectMake(width/2-R, height/2-R, 2*R, 2*R) cornerRadius:R] bezierPathByReversingPath];

    [pathappendPath:path1];

    CAShapeLayer *layer1 = [CAShapeLayer layer];

    layer1.path= path.CGPath;

    layer1.strokeColor= [UIColorlightGrayColor].CGColor;

    layer1.fillColor= [UIColorredColor].CGColor;

    [self.layeraddSublayer:layer1];

}

方法二:

- (void)drawRect:(CGRect)rect {

    [superdrawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGFloatradius =MIN(self.bounds.size.width,self.bounds.size.height)*0.5-60;

    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];

    CGFloattempY =NaviHeight+100;

    self.circleRect=CGRectMake((self.bounds.size.width-radius*2)*0.5, tempY, radius*2, radius*2);

    UIBezierPath *clipPath = [[UIBezierPath bezierPathWithRoundedRect:self.circleRect cornerRadius:radius] bezierPathByReversingPath];

    [pathappendPath:clipPath];

    CGContextAddPath(context, path.CGPath);

    CGContextSetFillColorWithColor(context, self.backColor.CGColor);

    CGContextFillPath(context);

}

相关文章

网友评论

      本文标题:在view里画个空心圆,相当于遮罩出一个空心圆

      本文链接:https://www.haomeiwen.com/subject/sfeinctx.html