用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. MongoDB 进阶模式设计

    原文链接:http://www.mongoing.com/mongodb-advanced-pattern-design 12月12日上午,TJ在开源中国的年终盛典会上分享了文档模型设计的进阶技巧,就 ...

  2. java判断部署项目使用的服务器类型

    有两种方式 1.项目引入portal-kernel.jar.项目运行时使用 根据返回boolean值判断类型! 2.自己写java类 package webService.ZFGX.service; ...

  3. C++ 编程错误记录

    C3646 未知重写说明符 两个头文件相互引用造成的问题

  4. 【AtCoder】CODE FESTIVAL 2017 Final

    A - AKIBA 模拟即可 代码 #include <bits/stdc++.h> #define fi first #define se second #define pii pair ...

  5. 对C转换说明符的误解以及关于数组的一些知识

    事实上,scanf()函数中%c并非是用来输入单个字符的,而是用来输入一组字符的. 例如: ]; scanf("%3c",a); 其中“3”规定了输入数据的宽度,当然宽度为“1”的 ...

  6. Google浏览器被360劫持

    最新更新!!! 新写的一个 Chrome浏览器被hao123劫持:    http://www.cnblogs.com/tanrong/p/7815494.html 这两个博客结合着看吧,它们几乎包括 ...

  7. java轻松实现无锁队列

    1.什么是无锁(Lock-Free)编程 当谈及 Lock-Free 编程时,我们常将其概念与 Mutex(互斥) 或 Lock(锁) 联系在一起,描述要在编程中尽量少使用这些锁结构,降低线程间互相阻 ...

  8. SQL中的坑

    一.where,group by,having --group by 和having 解释:前提必须了解sql语言中一种特殊的函数:聚合函数, 例如SUM, COUNT, MAX, AVG等.这些函数 ...

  9. CentOS和Windows下配置MySQL远程访问的教程

    CentOS和Windows下配置MySQL远程访问的教程   一.前言 由于实验在云服务器上跑的结果不是很理想.所以,现在切换到局域网服务器.因此,需要重新配置 Windows 服务器和 CentO ...

  10. conda设置Python虚拟环境

    conda设置Python虚拟环境     版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Co_zy/article/details/7741261 ...