zoukankan      html  css  js  c++  java
  • UIView中间透明周围半透明(四种方法)

    方法一


    #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);
    
    }
    

    这里写图片描述

  • 相关阅读:
    Linux第七节随笔 diff /uniq /stat
    部分命令积累
    Linux第六节随笔 输入输出重定向 、管道、通配符、wc / grep / tr / sort / cut / which /whereis /locate /find /
    单词记忆-3
    单词记忆-2
    特性
    Linux第五节随笔 /file / vim / suid /sgid sbit
    Linux第四节 组管理、用户管理、权限管理 / chmod /chown / umask / vim
    深入理解用户权限rwx
    Shell
  • 原文地址:https://www.cnblogs.com/-yun/p/5846269.html
Copyright © 2011-2022 走看看