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

    这里写图片描述

  • 相关阅读:
    TRIGGERS_监测系统_多表视图触发器—向原始数据报表中插入数据
    PROCEDURE_监测系统_原始数据报表数据生成存储过程—求每天平均值插入多表视图
    PROCEDURE_监测系统_数据备份存储过程—备份原始数据,每十分钟一条,取平均值
    TRIGGERS_监测系统_原始数据表触发器—调用告警信息存储过程
    PROCEDURE_监测系统_告警信息存储过程—产生告警信息插入告警表
    秒(s) 毫秒(ms) 微秒(μs) 纳秒(ns) 皮秒(ps)及Java获得 .
    new String(byte[])和byte[]toString() 的区别
    Java 线程池 .
    加载配置文件的方式
    Spring的PropertyPlaceholderConfigurer应用
  • 原文地址:https://www.cnblogs.com/-yun/p/5846269.html
Copyright © 2011-2022 走看看