美文网首页
UIView中间透明周围半透明(四种方法)

UIView中间透明周围半透明(四种方法)

作者: 王家小雷 | 来源:发表于2020-11-21 21:44 被阅读0次

    方法一


    #import "DrawView.h"
    
    @implementation DrawView
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            //设置 背景为clear
            self.backgroundColor = [UIColor clearColor];
            self.opaque = NO;
        }
        return self;
    }
    
    - (void)drawRect:(CGRect)rect {
    
        [[UIColor colorWithWhite:0 alpha:0.5] setFill];
        //半透明区域
        UIRectFill(rect);
    
        //透明的区域
        CGRect holeRection = CGRectMake(100, 200, 200, 200);
        /** union: 并集
         CGRect CGRectUnion(CGRect r1, CGRect r2)
         返回并集部分rect
         */
    
        /** Intersection: 交集
         CGRect CGRectIntersection(CGRect r1, CGRect r2)
         返回交集部分rect
         */
        CGRect holeiInterSection = CGRectIntersection(holeRection, rect);
        [[UIColor clearColor] setFill];
    
        //CGContextClearRect(ctx, <#CGRect rect#>)
        //绘制
        //CGContextDrawPath(ctx, kCGPathFillStroke);
        UIRectFill(holeiInterSection);
    
    }
    

    直接添加使用就行

        DrawView *drawView = [[DrawView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [self.view addSubview:drawView];
    
    
    这里写图片描述

    方法二


    #import "DrawViewArc.h"
    
    @implementation DrawViewArc
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor clearColor];
            self.opaque = NO;
        }
        return self;
    }
    
    - (void)drawRect:(CGRect)rect {
        //中间镂空的矩形框
        CGRect myRect =CGRectMake(100,100,200, 200);
    
        //背景
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:0];
        //镂空
        UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:myRect];
        [path appendPath:circlePath];
        [path setUsesEvenOddFillRule:YES];
    
        CAShapeLayer *fillLayer = [CAShapeLayer layer];
        fillLayer.path = path.CGPath;
        fillLayer.fillRule = kCAFillRuleEvenOdd;
        fillLayer.fillColor = [UIColor whiteColor].CGColor;
        fillLayer.opacity = 0.5;
        [self.layer addSublayer:fillLayer];
    
    }
    
    

    也是直接调用就行


    这里写图片描述

    方法三


    写到需要添加 透明圆的 view里

    - (void)addArc {
        //中间镂空的矩形框
        CGRect myRect =CGRectMake(100,100,200, 200);
    
        //背景
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:[UIScreen mainScreen].bounds cornerRadius:0];
        //镂空
        UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:myRect];
        [path appendPath:circlePath];
        [path setUsesEvenOddFillRule:YES];
    
        CAShapeLayer *fillLayer = [CAShapeLayer layer];
        fillLayer.path = path.CGPath;
        fillLayer.fillRule = kCAFillRuleEvenOdd;
        fillLayer.fillColor = [UIColor whiteColor].CGColor;
        fillLayer.opacity = 0.5;
        [self.view.layer addSublayer:fillLayer];
    
    }
    
    

    调用
    [self addArc];

    方法四


    #import "DrawArc.h"
    
    @implementation DrawArc
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor clearColor];
            self.opaque = NO;
        }
        return self;
    }
    
    - (void)drawRect:(CGRect)rect {
        //中间镂空的矩形框
        CGRect myRect =CGRectMake(100,100,200, 200);
    
        CGContextRef ctx = UIGraphicsGetCurrentContext();
    
        //背景色
        //[[UIColor colorWithPatternImage:[UIImage imageNamed:@"1.jpg"]] set];
        [[UIColor colorWithWhite:0 alpha:0.5] set];
        CGContextAddRect(ctx, rect);
        CGContextFillPath(ctx);
    
        //设置清空模式
        /**
         kCGBlendModeClear,
         kCGBlendModeCopy,
         kCGBlendModeSourceIn,
         kCGBlendModeSourceOut,
         kCGBlendModeSourceAtop,
         kCGBlendModeDestinationOver,
         kCGBlendModeDestinationIn,
         kCGBlendModeDestinationOut,
         kCGBlendModeDestinationAtop,
         kCGBlendModeXOR,
         kCGBlendModePlusDarker,
         kCGBlendModePlusLighter
         */
        CGContextSetBlendMode(ctx, kCGBlendModeClear);
    
        //画圆
        CGContextAddEllipseInRect(ctx, myRect);
    
        //填充
        CGContextFillPath(ctx);
    
    }
    
    
    这里写图片描述

    相关文章

      网友评论

          本文标题:UIView中间透明周围半透明(四种方法)

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