用UIBezierPath数组对UIView进行镂空处理
2016-07-08 23:11
381 查看
用UIBezierPath数组对UIView进行镂空处理
![](https://images2015.cnblogs.com/blog/607542/201607/607542-20160708225908499-1309253874.gif)
效果
![](https://images2015.cnblogs.com/blog/607542/201607/607542-20160708230018561-1466426884.png)
![](https://images2015.cnblogs.com/blog/607542/201607/607542-20160708230110999-1494733332.png)
![](https://images2015.cnblogs.com/blog/607542/201607/607542-20160708230223983-58364665.png)
源码
细节
![](https://images2015.cnblogs.com/blog/607542/201607/607542-20160708225908499-1309253874.gif)
效果
![](https://images2015.cnblogs.com/blog/607542/201607/607542-20160708230018561-1466426884.png)
![](https://images2015.cnblogs.com/blog/607542/201607/607542-20160708230110999-1494733332.png)
![](https://images2015.cnblogs.com/blog/607542/201607/607542-20160708230223983-58364665.png)
源码
// // 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, 91)]; [bezierPath addLineToPoint: CGPointMake(57.04, 169.5)]; [bezierPath addLineToPoint: CGPointMake(18.5, 91)]; [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
细节
![](https://images2015.cnblogs.com/blog/607542/201607/607542-20160708230925889-1129803999.png)
相关文章推荐
- Unique Binary Search Trees I & II
- JQuert选择器
- iOS开发_限制UITextView的最大字数
- Codeforces Round #360 (Div. 1)C - The Values You Can Make
- 新浪云sae 邮件服务 quicksend()
- Codeforces 625 . A Guest From the Past
- UINavgationContoller上面的UIBarButtonItem之间的间距调整
- 封装一个UILabel圆形边框显示进度
- POJ 2785 4 Values whose Sum is 0
- 用ndk-build 工具手动生成编译android原生程序
- RMQ+二分 - CF 689D Friends and Subsequences
- 蓝牙uuid
- UVA - 10420 List of Conquests
- CSS3 Media Queries详细介绍和使用实例
- 定义css设备类型-Media Queries图表简介及使用方法
- Android Studio官方文档之使用布局编辑器来设计UI界面
- ERROR 1366 (HY000): Incorrect string value: '\xC6\xF4\xD3\xC3' for column 'name' at row 1
- UOJ #113. 【UER #2】手机的生产
- 225. Implement Stack using Queues
- ntp的错误 no server suitable for synchronization found