方法一


#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中间透明周围半透明(四种方法)的更多相关文章

  1. iOS设置圆角的四种方法

    小小圆角问题,正常情况下,我们不需要过多关心,但当屏幕内比较多的时候,还是有必要了解下性能问题的 一.设置CALayer的cornerRadius 这是最常用的,也是最简单的. cornerRadiu ...

  2. 两个变量交换的四种方法(Java)

    对于两种变量的交换,我发现四种方法,下面我用Java来演示一下. 1.利用第三个变量交换数值,简单的方法. (代码演示一下) class TestEV //创建一个类 { public static ...

  3. 织梦DedeCMS模板防盗的四种方法

    织梦(DedeCMS)模板也是一种财富,不想自己辛辛苦苦做的模板被盗用,在互联网上出现一些和自己一模一样的网站,就需要做好模板防盗.本文是No牛收集整理自网络,不过网上的版本都没有提供 Nginx 3 ...

  4. 让一个图片在div中居中(四种方法)

    第一种方法: <div class="title"> <div class="flag"></div> <div cl ...

  5. 运行jar应用程序引用其他jar包的四种方法

    转载地址:http://www.iteye.com/topic/332580 大家都知道一个java应用项目可以打包成一个jar,当然你必须指定一个拥有main函数的main class作为你这个ja ...

  6. java中定时器的四种方法

    package com.lid; import java.util.Calendar; import java.util.Date; import java.util.Timer; import ja ...

  7. Angular--页面间切换及传值的四种方法

    1. 基于ui-router的页面跳转传参(1) 在AngularJS的app.js中用ui-router定义路由,比如现在有两个页面,一个页面(producers.html)放置了多个produce ...

  8. MYSQL获取自增ID的四种方法

    MYSQL获取自增ID的四种方法 1. select max(id) from tablename 2.SELECT LAST_INSERT_ID() 函数 LAST_INSERT_ID 是与tabl ...

  9. linux下配置ip地址四种方法(图文方法)

    主要是用第四种方法 (1)Ifconfig命令  第一种使用ifconfig命令配置网卡的ip地址.此命令通常用来零时的测试用,计算机启动后 ip地址的配置将自动失效.具体用法如下.Ipconfig  ...

随机推荐

  1. Chrome 浏览器提示adobe flash player不是最新版本

    百度下载最新版. 刷新 Chrome浏览器.更换其他的浏览器. 刷新过后,无效. 打开Flash Player 检查安装的版本. IE会链接到官网. 对应自己的系统.找到自己用的浏览器.看是否一致. ...

  2. cs11_c++_lab4a

    SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...

  3. Productivity Power Tools 动画演示(转)

    Productivity Power Tools 是微软官方推出的 Visual Studio 扩展,被用以提高开发人员生产率.它的出现一定程度上弥补和完善了 Visual Studio 自身的不足, ...

  4. ios10 no route to host

    IdTCPClient1 ios10 no route to host IOS 9 正常 IOS10 出错,参考下面的设置解决问题 http://www.sh-huhe.com/solution/14 ...

  5. Redis配置文件参数说明

    Redis配置文件参数说明   1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no 2. 当Redis以守护进程方式运行时,Redis ...

  6. 优化 从Draw Calls到GC

    原文出处: 慕容小匹夫的博客(@慕容小匹夫)   欢迎分享原创到伯乐头条 前言: 刚开始写这篇文章的时候选了一个很土的题目...<Unity3D优化全解析>.因为这是一篇临时起意才写的文章 ...

  7. Opencl 并行求和

    上周尝试用opencl求极大值,在网上查到大多是求和,所谓的reduction算法.不过思路是一样的. CPP: ; unsigned ; ; ; int nGroup = nGroupSize / ...

  8. 虚拟机virtualBox设置共享文件后,linux配置

    1.在/mnt下创建共享目录 mkdir /mnt/share 2.关联外部目录 mount -t vboxsf 共享文件夹名 /mnt/share/ 如:mount -t vboxsf BaiduS ...

  9. Pureftpd

    在lnmp目录下运行./pureftpd.sh 如下:[root@cloud lnmp1.3-full]# 执行./pureftpd.sh 使用命令 Install Pure-FTPd complet ...

  10. Sql 触发器禁用和启用

    --启用or禁用指定表所有外键约束 alter table tbname NOCHECK constraint all alter table tbname CHECK constraint all ...