用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进行镂空处理的更多相关文章

  1. UIView中间透明周围半透明(四种方法)

    方法一 #import "DrawView.h" @implementation DrawView - (instancetype)initWithFrame:(CGRect)fr ...

  2. IOS 作业项目(1) 关灯游戏 (百行代码搞定)

    1,准备工作,既然要开关灯,就需要确定灯的灯的颜色状态 首先想到的是扩展UIColor

  3. 读取svg图片为UIBezierPath,开心做动画

    动画预览 先扯淡 最近手痒又想整点动画玩玩,但是想了几个主意发现稍微复杂一点的手写都一定会累爆.这篇文章记录一下今天折腾的一个方案.说来简单,就是用矢量设计工具舒舒服服的做好设计,然后输出成 svg ...

  4. 11-UIKit(Storyboard、View的基本概念、绘制图形、UIBezierPath)

    目录: 1. Storyboard 2. Views 3. View的基本概念介绍 4. 绘制图形 5. UIBezierPath 回到顶部 1. Storyboard 1.1 静态表视图 1)Sec ...

  5. iOS学习——UIView的研究

    在iOS开发中,我们知道有一个共同的基类——NSObject,但是对于界面视图而言,UIView是非常重要的一个类,UIView是很多视图控件的基类,因此,对于UIView的学习闲的非常有必要.在iO ...

  6. iOS动画-从UIView到Core Animation

    首先,介绍一下UIView相关的动画. UIView普通动画: [UIView beginAnimations: context:]; [UIView commitAnimations]; 动画属性设 ...

  7. iOS性能优化-数组、字典便利时间复杂

    上图是几种时间复杂度的关系,性能优化一定程度上是为了降低程序执行效率减低时间复杂度. 如下是几种时间复杂度的实例: O(1) return array[index] == value; 复制代码 O( ...

  8. 使用UIBezierPath绘制图形

    当需要画图时我们一般创建一个UIView子类, 重写其中的drawRect方法 再drawRect方法中利用UIBezierPath添加画图 UIBezierPath的使用方法: (1)创建一个Bez ...

  9. iOS 自定义方法 - UIView扩展

    示例代码 //#import <UIKit/UIKit.h>@interface UIView (LPCView)/** 上 */@property CGFloat top;/** 下 * ...

随机推荐

  1. 基于Prometheus的Pushgateway实战

    一.Pushgateway 简介 Pushgateway 是 Prometheus 生态中一个重要工具,使用它的原因主要是: Prometheus 采用 pull 模式,可能由于不在一个子网或者防火墙 ...

  2. IIS开多个HTTPS站点

    默认情况一个服务器的IIS只能绑定一个HTTPS也就是443端口 要实现多个站点对应HTTPS只能更改IIS配置 地址:C:\Windows\system32\inetsrv\config\appli ...

  3. Jupyter notebook安装与使用

    Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言. 安装 安装python 3 pip安装 pip3 install - ...

  4. JMS(Java消息服务)

    JMS即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM:指的是利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来 ...

  5. Flume分布式日志收集系统

    1.flume是分布式的日志收集系统,把收集来的数据传送到目的地去.2.flume里面有个核心概念,叫做agent.agent是一个java进程,运行在日志收集节点.通过agent接收日志,然后暂存起 ...

  6. PHP文件上传及下载源码

    一.文件上传 前台页面: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  7. ubuntu ifconfig只有lo没有ens33的问题

    如果ifconfig只显示了lo, ifconfig -a 却正常显示ens33.那么可以按照如下的操作: service network-manager stop rm /var/lib/Netwo ...

  8. Db2与Oracle的区别

    这个部分是自己随便整理下. 在工作上需要,公司需要DB2兼容Oracle. 不懂DB2与Oracle的细节,这里努力整理,以后精通了再回来重新修改. https://www.2cto.com/data ...

  9. R语言编程艺术(4)R对数据、文件、字符串以及图形的处理

    本文对应<R语言编程艺术> 第8章:数学运算与模拟: 第10章:输入与输出: 第11章:字符串操作: 第12章:绘图 =================================== ...

  10. 023.Zabbix自定义(邮箱)脚本告警-02

    待补充 有需要,请留言!