一. 简单做一个画板

1. 建立一个UIView类

2. 在.m里建立一个延展

3. 分别定义一个起点, 一个终点的结构体属性 . 在建立一个存储路径的数组

@interface DrawView ()
{
CGPoint _startPoint;
CGPoint _endPoint;
} @property (nonatomic, strong) NSMutableArray *pathArray; @end

4. 懒加载数组

- (NSMutableArray *)pathArray
{
if (_pathArray == nil) {
_pathArray = [NSMutableArray array];
}
return _pathArray;
}

5. 开始绘制

(1) 起点

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 获取起点
_startPoint = [[touches anyObject] locationInView:self]; // 创建贝瑟尔路径
UIBezierPath *path = [UIBezierPath bezierPath];
// 起点
[path moveToPoint:_startPoint]; // 添加到数组中
[self.pathArray addObject:path]; }

(2) 终点

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
_endPoint = [[touches anyObject] locationInView:self]; // 终点
UIBezierPath *path = [self.pathArray lastObject]; [path addLineToPoint:_endPoint]; [self setNeedsDisplay];
}

6. 重写  - (void)drawRect:(CGRect)rect 方法

- (void)drawRect:(CGRect)rect {
// Drawing code for (UIBezierPath *path in self.pathArray) {
// 设置颜色
[[UIColor orangeColor] set];
// 设置粗度
path.lineWidth = ;
// 渲染
[path stroke];
} }

二. 绘制饼状图

注 : 为了好看, 我随即分成了大小颜色都不同的99份

代码实现

#import "DrawView.h"

#define kcolor arc4random()% 256 / 255.0

@implementation DrawView

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code NSMutableArray *array = [NSMutableArray array];
int sum = ;
for (int i = ; i< ; i ++) {
int num = arc4random()% ;
sum += num;
NSNumber *number = [NSNumber numberWithInt:num];
[array addObject:number];
}
// 记录起点和终点
CGFloat start = ;
CGFloat end = ;
// 遍历数据
for (NSNumber *num in array) { end = num.floatValue / sum * M_PI * ; UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(, ) radius: startAngle:start endAngle:start + end clockwise:YES];
// 关闭路径
[path addLineToPoint:CGPointMake(, )]; [[UIColor colorWithRed:kcolor green:kcolor blue:kcolor alpha:] set];
[path closePath];
[path fill];
start += end; }

@end

 

三. 截图用法

1. 首先要有一个imageView, 所以在ViewContorller先建立一个UIImageView的属性 将背景的图片设置出来

2. 建立一个继承自UIView的类

3. 在这个UIView类.m里实现截图 (.h中写了一个block块用于传递路径)

.h 代码实现

// 传递路径
typedef void(^block)(UIBezierPath *p); @interface MyView : UIView @property (nonatomic, copy) block myblock; @end

.m 代码实现

#import "MyView.h"

@interface MyView ()

// 保存路径
@property (nonatomic, strong) NSMutableArray *pathArray; @end @implementation MyView - (NSMutableArray *)pathArray
{
if (! _pathArray) {
_pathArray = [NSMutableArray array];
}
return _pathArray;
} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 1. 获取起始点 并且创建路径
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:[[touches anyObject] locationInView:self]]; // 2. 装入数组
[self.pathArray addObject:path]; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 1. 获取终点 (不止一个)
UIBezierPath *path = [self.pathArray lastObject];
[path addLineToPoint:[[touches anyObject] locationInView:self]]; // 2. 调用drawrect
[self setNeedsDisplay]; } - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// 封闭路径
UIBezierPath *path = [self.pathArray lastObject];
[path closePath]; [self setNeedsDisplay];
// 调用block路径
self.myblock(path);
} // Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
for (UIBezierPath *path in self.pathArray) {
path.lineWidth = ;
[[UIColor orangeColor] set];
[path stroke]; }
} @end

4. 在ViewController中中回调路径

#import "ViewController.h"

#import "MyImageView.h"

#import "MyView.h"

@interface ViewController ()

@property (nonatomic, strong) UIImageView *imageView;

@property (nonatomic, strong) MyView *myView;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// 获取图片
// __block全局区
__block UIImage *image = [UIImage imageNamed:@""];
self.imageView = [[UIImageView alloc] initWithImage:image];
_imageView.frame = CGRectMake(, , image.size.width, image.size.height);
[self.view addSubview: _imageView]; self.myView = [[MyView alloc] initWithFrame:self.imageView.frame];
_myView.backgroundColor = [UIColor colorWithRed: green: blue: alpha:];
[self.view addSubview:_myView]; // 剪切 __weak typeof(self) weakSelf = self;
_myView.myblock = ^(UIBezierPath *p){ // 获取上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, );
// 路径
UIBezierPath *path = p;
// 剪切
[path addClip];
// 绘制
[image drawAtPoint:CGPointZero];
// 获取剪切后的图片
image = UIGraphicsGetImageFromCurrentImageContext();
// 结束上下文
UIGraphicsEndImageContext();
// 给imageView赋值
weakSelf.imageView.image = image; };
} @end

QuartZ2D __ 简单用法 1的更多相关文章

  1. CATransition(os开发之画面切换) 的简单用法

    CATransition 的简单用法 //引进CATransition 时要添加包“QuartzCore.framework”,然后引进“#import <QuartzCore/QuartzCo ...

  2. jquery.validate.js 表单验证简单用法

    引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...

  3. NSCharacterSet 简单用法

    NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ...

  4. [转]Valgrind简单用法

    [转]Valgrind简单用法 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html Valgrind的主要作者Julian S ...

  5. Oracle的substr函数简单用法

    substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H'  *从字符串第一个字符开始截取长度为1的字符串 subst ...

  6. Ext.Net学习笔记19:Ext.Net FormPanel 简单用法

    Ext.Net学习笔记19:Ext.Net FormPanel 简单用法 FormPanel是一个常用的控件,Ext.Net中的FormPanel控件同样具有非常丰富的功能,在接下来的笔记中我们将一起 ...

  7. TransactionScope简单用法

    记录TransactionScope简单用法,示例如下: void Test() { using (TransactionScope scope = new TransactionScope()) { ...

  8. WPF之Treeview控件简单用法

    TreeView:表示显示在树结构中分层数据具有项目可展开和折叠的控件 TreeView 的内容是可以包含丰富内容的 TreeViewItem 控件,如 Button 和 Image 控件.TreeV ...

  9. listActivity和ExpandableListActivity的简单用法

    http://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html 今天自己简单的总结了listActivity和Expandable ...

随机推荐

  1. linux安装php

    接上篇:linux安装apache 一.安装php 先安装libxml2库 [root@ctxsdhy package]# yum -y install libxml2-devel 最新地址在:htt ...

  2. 如何生成每秒百万级别的 HTTP 请求?

    第一篇:<如何生成每秒百万级别的 HTTP 请求?> 第二篇:<为最佳性能调优 Nginx> 第三篇:<用 LVS 搭建一个负载均衡集群> 本文是构建能够每秒处理 ...

  3. bzoj 4016: [FJOI2014]最短路径树问题

    bzoj4016 最短路路径问题 Time Limit: 5 Sec Memory Limit: 512 MB Description 给一个包含n个点,m条边的无向连通图.从顶点1出发,往其余所有点 ...

  4. C# 数据批量插入到数据库SqlBulkCopy(源数据类型:List<T> Or DataTable)

      /*_____________________ List<T>类型数据 To Sql_______________________________*/ /// <summary& ...

  5. OpenStack三种类型的NAT转换

    SNAT SNAT即源网络地址转换,这个NAT路由修改IP包包头中的源IP地址.SNAT功能通常用于让只具有私有IP地址的主机能够访问外网,比如,多个PC使用路由器共享上网,每个PC都配置了内网IP, ...

  6. Vue.js 整理笔记

    以前我们用Jquery进行dom的操作,虽然熟悉后开发效率很高,但是如果多个控件的相互操作多的情况下,还是会乱.相比之下,Vue的使用更加清晰,通过虚拟dom将数据绑定,而且组件化和路由的帮助下,让整 ...

  7. python模块简介

    模块:用代码实现了某个功能的代码集合,功能模块化,节省时间,提高效率 一.模块的导入 导入模块,其实就是告诉python解释器去解释相应的.py文件 • 导入.py文件,解释器解释该.py文件 • 倒 ...

  8. MAC OS升级到10.11(OS X EICAPTION)之后CocoaPods不能正常使用的问题解决

    昨晚回家之后开始升级系统到10.11,下载了一整个晚上之后终于在早上下载完毕,早上带到公司,想查一个第三方库的时候却遇到了问题: guoyufudeMacBook-Pro:~ GuoYufu$ pod ...

  9. Thrift的TJsonProtocol协议分析

    Thrift协议实现目前有二进制协议(TBinaryProtocol),紧凑型二进制协议(TCompactProtocol)和Json协议(TJsonProtocol). 前面的两篇文字从编码和协议原 ...

  10. 11.11光棍节工作心得——github/MVP

    11.11光棍节工作心得 1.根据scrum meeting thirdday中前辈的指导进行学习 我在博客中贴了链接,竟然TrackBack引来了原博主,