用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. LeetCode(17):电话号码的字母组合

    Medium! 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23& ...

  2. aria2

    在之前我们已经介绍了通过uGet使用aria2来进行下载,但是这样只是使用aria2最简单的功能,现在我们来介绍一下aria2的常用命令 简单篇: 一般使用使用 aria2 下载文件,只需在命令后附加 ...

  3. /dev/null和/dev/zero的区别

    /dev/null,外号叫无底洞,你可以向它输出任何数据,它通吃,并且不会撑着!/dev/zero,是一个输入设备,你可你用它来初始化文件. /dev/null------它是空设备,也称为位桶(bi ...

  4. Mybatis输入映射和输出映射

    本节内容: 输入参数映射 输出映射 resultMap Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心. 一.环境准备 复制 ...

  5. IdentityServer4使用EFCore生成MySql时的小bug

    EFCore生成PersistedGrantDbContextModelSnapshot的时候发现 b.Property<string>("Data") .IsRequ ...

  6. Authentication(Spring Security 认证笔记)

    这篇文章是对Spring Security的Authentication模块进行一个初步的概念了解,知道它是如何进行用户认证的 考虑一个大家比较熟悉的标准认证过程: 1.用户使用username和pa ...

  7. 远程执行shell

    目前我的需求是在我的hadoop集群搭建起来前,能定时做一下简易指标的监控,目前我的方案就是在我的server机上,定时执行远程脚本出指标数据,然后通过python脚本发送邮件. 远程执行脚本如下: ...

  8. mysql排序数据

    一:order by的普通使用 1.介绍 当使用SELECT语句查询表中的数据时,结果集不按任何顺序进行排序.要对结果集进行排序,请使用ORDER BY子句. ORDER BY子句允许: 对单个列或多 ...

  9. 洛谷P2894 [USACO08FEB]酒店Hotel [线段树]

    题目传送门 酒店 题目描述 The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and ...

  10. MIT-6.828-JOS-lab2:Memory management

    MIT-6.828 Lab 2: Memory Management实验报告 tags:mit-6.828 os 概述 本文主要介绍lab2,讲的是操作系统内存管理,从内容上分为三部分: 第一部分讲的 ...