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);
}
UIView中间透明周围半透明(四种方法)的更多相关文章
- iOS设置圆角的四种方法
小小圆角问题,正常情况下,我们不需要过多关心,但当屏幕内比较多的时候,还是有必要了解下性能问题的 一.设置CALayer的cornerRadius 这是最常用的,也是最简单的. cornerRadiu ...
- 两个变量交换的四种方法(Java)
对于两种变量的交换,我发现四种方法,下面我用Java来演示一下. 1.利用第三个变量交换数值,简单的方法. (代码演示一下) class TestEV //创建一个类 { public static ...
- 织梦DedeCMS模板防盗的四种方法
织梦(DedeCMS)模板也是一种财富,不想自己辛辛苦苦做的模板被盗用,在互联网上出现一些和自己一模一样的网站,就需要做好模板防盗.本文是No牛收集整理自网络,不过网上的版本都没有提供 Nginx 3 ...
- 让一个图片在div中居中(四种方法)
第一种方法: <div class="title"> <div class="flag"></div> <div cl ...
- 运行jar应用程序引用其他jar包的四种方法
转载地址:http://www.iteye.com/topic/332580 大家都知道一个java应用项目可以打包成一个jar,当然你必须指定一个拥有main函数的main class作为你这个ja ...
- java中定时器的四种方法
package com.lid; import java.util.Calendar; import java.util.Date; import java.util.Timer; import ja ...
- Angular--页面间切换及传值的四种方法
1. 基于ui-router的页面跳转传参(1) 在AngularJS的app.js中用ui-router定义路由,比如现在有两个页面,一个页面(producers.html)放置了多个produce ...
- MYSQL获取自增ID的四种方法
MYSQL获取自增ID的四种方法 1. select max(id) from tablename 2.SELECT LAST_INSERT_ID() 函数 LAST_INSERT_ID 是与tabl ...
- linux下配置ip地址四种方法(图文方法)
主要是用第四种方法 (1)Ifconfig命令 第一种使用ifconfig命令配置网卡的ip地址.此命令通常用来零时的测试用,计算机启动后 ip地址的配置将自动失效.具体用法如下.Ipconfig ...
随机推荐
- vbox 不识别u盘的问题
usb设备 ->启用usb设备 ->启用usb2.0(ehci)控制器 添加usb筛选器 给筛选器起个名字
- Python面试题
1.Python装饰器 详情 2.设置多个Python项目使用不同版本的Python和第三方库 使用PyEnv 详情 3.PEP8 详情 4.参数传递 按引用传递 5.列表解析,字典解析 详情 6.列 ...
- 简单的内网存活主机ip扫描
@echo offset /a ti = 1:startif %ti% == 10 goto endping 192.168.1.%ti% -n 1 -w 20set /a ti = ti + 1go ...
- Hibernate 中出现 users is not mapped 问题 (转)
今天晚上自己试着用Hibernate去搭建一个Web工程,然后去实现一个简单的登录. 通过Hibernate 做查询操作的时候总是报出这样的错: ...
- NTFS u盘支持 - centos6
uname -r bash-4.1# uname -r2.6.32-431.el6.x86_64 ---- http://repository.it4i.cz/mirrors/repoforge/re ...
- ASP.NET Misconfiguration: Excessive Session Timeout
Abstract: An overly long authentication timeout gives attackers more time to potentially compromise ...
- React Native 实现页面动态切换
第一步. 初始化子View constructor(props){ super(props); this.state = { isChange : true, itemView : (<Text ...
- php限定时间内同一ip只能访问一次
建立一个数据表 CREATE TABLE `clicks` ( `ip` INT UNSIGNED NOT NULL , `time1` INT UNSIGNED NOT NULL , `time2` ...
- js计时事件
通过在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行,我们称之为计时事件. 1. setTimeout()--暂停指定的时间后执行指定的代码 clearTimeout ()--停止se ...
- jQuery Mobile的基本使用
本人是软件开发的初学者,总结了一点点日常所学,记录在此,主要目的是鼓励自己坚持学习,怕有一天忘记了,还能复习曾经学过的知识点. 如有大神路过为我指点迷津,纠正改错更是感激不尽,但请不要喷我这个菜鸟!谢 ...