用UIBezierPath数组对UIView进行镂空处理
用UIBezierPath数组对UIView进行镂空处理

效果



源码
//
// CutOutClearView.h
// CutOutMaskView
//
// Created by YouXianMing on 16/7/8.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> @interface CutOutClearView : UIView @property (nonatomic, strong) UIColor *fillColor;
@property (nonatomic, strong) NSArray <UIBezierPath *> *paths; @end
//
// CutOutClearView.m
// CutOutMaskView
//
// Created by YouXianMing on 16/7/8.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "CutOutClearView.h" @implementation CutOutClearView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.fillColor = [UIColor whiteColor];
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
} return self;
} - (void)drawRect:(CGRect)rect { [super drawRect:rect]; [self.fillColor setFill];
UIRectFill(rect); CGContextRef context = UIGraphicsGetCurrentContext(); for (UIBezierPath *path in self.paths) { CGContextAddPath(context, path.CGPath);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextFillPath(context);
}
} @end
//
// ViewController.m
// CutOutClearView
//
// Created by YouXianMing on 16/7/8.
// Copyright © 2016年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "CutOutClearView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = [UIImage imageNamed:@"bg.png"];
imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:imageView]; NSMutableArray *paths = [NSMutableArray array]; {
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(57.04, 31.19)];
[bezierPath addLineToPoint: CGPointMake(125.55, 12.5)];
[bezierPath addLineToPoint: CGPointMake(185.5, )];
[bezierPath addLineToPoint: CGPointMake(57.04, 169.5)];
[bezierPath addLineToPoint: CGPointMake(18.5, )];
[bezierPath addLineToPoint: CGPointMake(57.04, 31.19)];
[bezierPath closePath];
[paths addObject:bezierPath];
} {
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(46.5, 245.5)];
[bezierPath addLineToPoint: CGPointMake(137.5, 272.5)];
[bezierPath addLineToPoint: CGPointMake(137.5, 211.5)];
[bezierPath addLineToPoint: CGPointMake(90.5, 196.5)];
[bezierPath addLineToPoint: CGPointMake(46.5, 211.5)];
[bezierPath addLineToPoint: CGPointMake(46.5, 245.5)];
[bezierPath closePath];
[paths addObject:bezierPath];
} CutOutClearView *cutOutView = [[CutOutClearView alloc] initWithFrame:self.view.bounds];
cutOutView.fillColor = [UIColor redColor];
cutOutView.paths = paths;
// [self.view addSubview:cutOutView];
imageView.maskView = cutOutView;
} @end
细节

用UIBezierPath数组对UIView进行镂空处理的更多相关文章
- UIView中间透明周围半透明(四种方法)
方法一 #import "DrawView.h" @implementation DrawView - (instancetype)initWithFrame:(CGRect)fr ...
- IOS 作业项目(1) 关灯游戏 (百行代码搞定)
1,准备工作,既然要开关灯,就需要确定灯的灯的颜色状态 首先想到的是扩展UIColor
- 读取svg图片为UIBezierPath,开心做动画
动画预览 先扯淡 最近手痒又想整点动画玩玩,但是想了几个主意发现稍微复杂一点的手写都一定会累爆.这篇文章记录一下今天折腾的一个方案.说来简单,就是用矢量设计工具舒舒服服的做好设计,然后输出成 svg ...
- 11-UIKit(Storyboard、View的基本概念、绘制图形、UIBezierPath)
目录: 1. Storyboard 2. Views 3. View的基本概念介绍 4. 绘制图形 5. UIBezierPath 回到顶部 1. Storyboard 1.1 静态表视图 1)Sec ...
- iOS学习——UIView的研究
在iOS开发中,我们知道有一个共同的基类——NSObject,但是对于界面视图而言,UIView是非常重要的一个类,UIView是很多视图控件的基类,因此,对于UIView的学习闲的非常有必要.在iO ...
- iOS动画-从UIView到Core Animation
首先,介绍一下UIView相关的动画. UIView普通动画: [UIView beginAnimations: context:]; [UIView commitAnimations]; 动画属性设 ...
- iOS性能优化-数组、字典便利时间复杂
上图是几种时间复杂度的关系,性能优化一定程度上是为了降低程序执行效率减低时间复杂度. 如下是几种时间复杂度的实例: O(1) return array[index] == value; 复制代码 O( ...
- 使用UIBezierPath绘制图形
当需要画图时我们一般创建一个UIView子类, 重写其中的drawRect方法 再drawRect方法中利用UIBezierPath添加画图 UIBezierPath的使用方法: (1)创建一个Bez ...
- iOS 自定义方法 - UIView扩展
示例代码 //#import <UIKit/UIKit.h>@interface UIView (LPCView)/** 上 */@property CGFloat top;/** 下 * ...
随机推荐
- html圈圈
<html> <head> <meta charset="utf-8" /> <link href="images/style. ...
- 4.类型设计规范《.NET设计规范》
类是引用类型的一般情况,占了框架中的大多情况,类的流行归于它支持面向对象的特征,以及它的普遍的适用性,基类和抽象类是两个特殊的逻辑分组,它们与扩张性有关. 由于CLR不支持多继承,接口类型可以用来模拟 ...
- Linux系统运维笔记(三),设置IP和DNS
Linux系统运维笔记(三),设置IP和DNS 手工配置静态的IP地址 也就是手工配置IP地址.子网掩码.网关和DNS. vi /etc/sysconfig/network-scripts/ifcfg ...
- Nginx + PHP(php-fpm)遇到的502 Bad Gateway错误
我一个统计程序估计要跑1分多钟以上 查看了一个php-fpm 配置文件 [13-Oct-2013 12:06:07] WARNING: [pool www] child 7458, script '/ ...
- mysql排序数据
一:order by的普通使用 1.介绍 当使用SELECT语句查询表中的数据时,结果集不按任何顺序进行排序.要对结果集进行排序,请使用ORDER BY子句. ORDER BY子句允许: 对单个列或多 ...
- 025 Spark中的广播变量原理以及测试(共享变量是spark中第二个抽象)
一:来源 1.说明 为啥要有这个广播变量呢. 一些常亮在Driver中定义,然后Task在Executor上执行. 如果,有多个任务在执行,每个任务需要,就会造成浪费. 二:共享变量的官网 1.官网 ...
- Python - 从列表中取随机数
题目是:从一个有序列表中任取几个值组成新的列表 以下有2种思路去实现 1. 把那列表任意排列,截取尾巴上面的指定长度 import random total = 100 onetime = 7 x_l ...
- git初级浅入其常用操作
1. git init 我们从初始化一个仓库开始,通过此命令可以初始化一个仓库 git init 首先我们在当前目录下创建一个目录pratice和一个文件test.js mkdir pratice c ...
- [代码审计]yxcms从伪xss到getshell
0x00 前言 这篇文章首发于圈子,这里作为记录一下. 整个利用链构造下来是比较有趣的,但实际渗透中遇到的几率比较少. 此次审的是yxcms 1.4.6版本,应该是最后一个版本了吧? 0x01 从任意 ...
- Scrapy 模拟登陆知乎--抓取热点话题
工具准备 在开始之前,请确保 scrpay 正确安装,手头有一款简洁而强大的浏览器, 若是你有使用 postman 那就更好了. Python 1 scrapy genspid ...