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

看效果


方法一:

代码如下,自定义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);

}

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

友情链接更多精彩内容