用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;/** 下 * ...
随机推荐
- 更换网页tab标题图标
在首页HTML文件中,加入link命令,<link>是放在<head>与</head>之间 例如下面这样: <HEAD><link rel = & ...
- hdu 1754 线段树(单点替换 区间最值)
Sample Input5 61 2 3 4 5Q 1 5 //1-5结点的最大值U 3 6 //将点3的数值换成6Q 3 4Q 4 5U 2 9Q 1 5 Sample Output5659 # i ...
- codewar 上做练习的一些感触
废话 在[codewar][1]上做练习,每次都是尽量快速地做完,然后赶着去看排名里面clever分最高的solution,看完每次都要感叹一下人家怎么可以写得这么简洁,甚至有一次我用了一段大约七八行 ...
- WebApi 文档Swagger
NET WebApi 文档Swagger中度优化 本文版权归博客园和作者吴双本人共同所有,转载和爬虫请注明原文地址:www.cnblogs.com/tdws 写在前面 在后台接口开发中,接口文 ...
- VS C++ 并发编程
1.VS2012及以上版本,支持C++11 thread类的并发编程. 相关材料可以参考博客:http://www.cnblogs.com/rangozhang/p/4468754.html 2.但对 ...
- getImplementationVersion-获取版本号
在工作中会遇到这个方法的使用,就记录一下. 一:getImplementationVersion 1. 方法 java.lang.Package.getImplementationVersion() ...
- 电子邮件-TCP
参考下图,由于电子邮件是最早应用于网络上的服务,而早期网络的不可靠性,才设计了TCP协议,而邮件必须要保证正确传输,而非高速,所以早期的电子邮件协议 全是基于TCP的,一直持续到现在.
- 容器(Container)Frames和Panels
Frames 1)是Window的子类 2)具有标题和缩放角 3)从容器继承并以add方式添加组件 4)能以字符串规定的标题来创建不可见框架对象 5)能将BorderLayout当做缺省布局管理器 6 ...
- 括弧匹配检验(check.cpp)
[问题描述] 假设表达式中允许包含两种括号:圆括号和方括号,其嵌套的顺序随意,如([ ]())或[([ ][ ])]等为正确的匹配,[( ])或([ ]( )或 ( ( ) ) )均为错 ...
- Codeforces Round #549 (Div. 1) 题解
link 前几天补完了某一场很早以前的div1,突然想来更博客,于是就有了这篇文章 A The Beatles 显然若起点和第一次到达的位置距离为 d ,那么经过的不同站点数为 $\frac{nk}{ ...