iOS开发 - Quartz2D画图
Quartz 2D简单介绍
是一个二维画图引擎,同一时候支持iOS和Mac系统
Quartz 2D能完毕的工作
绘制图形 : 线条\三角形\矩形\圆\弧等
绘制文字
绘制\生成图片(图像)
读取\生成PDF
截图\裁剪图片
自己定义UI控件
… …
drawRect:方法的使用
常见图形的绘制:线条、多边形、圆
画图状态的设置:文字颜色、线宽等
图形上下文状态的保存与恢复
图形上下文栈
为了便于搭建美观的UI界面,iOS提供了UIKit框架,里面有各种各样的UI控件
UILabel:显示文字
UIImageView:显示图片
UIButton:同一时候显示图片和文字(能点击)
… …
Quartz2D价值
利用UIKit框架提供的控件,拼拼凑凑。能搭建和现实一些简单、常见的UI界面
可是,有些UI界面极其复杂、并且比較个性化,用普通的UI控件无法实现。这时能够利用Quartz2D技术将控件内部的结构画出来,自己定义控件的样子
事实上,iOS中大部分控件的内容都是通过Quartz2D画出来的
因此。Quartz2D在iOS开发中非常重要的一个价值是:自己定义view(自己定义UI控件
Quartz2D的API是纯C语言的
Quartz2D的API来自于Core Graphics框架
数据类型和函数基本都以CG作为前缀
CGContextRef
CGPathRef
CGContextStrokePath(ctx);
……
图形上下文(Graphics Context)
图形上下文(Graphics Context):是一个CGContextRef类型的数据
图形上下文的作用
保存画图信息、画图状态
决定绘制的输出目标(绘制到什么地方去?)
(输出目标能够是PDF文件、Bitmap或者显示器的窗体上)
同样的一套画图序列,指定不同的Graphics Context。就可将同样的图像绘制到不同的目标上
Quartz2D提供了以下几种类型的Graphics Context:
Bitmap Graphics Context
PDF Graphics Context
Window Graphics Context
Layer Graphics Context
Printer Graphics Context
Quartz2D自己定义view
怎样利用Quartz2D自己定义view?(自己定义UI控件)
怎样利用Quartz2D绘制东西到view上?
首先,得有图形上下文。由于它能保存画图信息,并且决定着绘制到什么地方去
其次,那个图形上下文必须跟view相关联,才干将内容绘制到view上面
自己定义view的步骤
新建一个类,继承自UIView
实现- (void)drawRect:(CGRect)rect方法,然后在这种方法中
取得跟当前view相关联的图形上下文
绘制对应的图形内容
利用图形上下文将绘制的全部内容渲染显示到view上面
drawRect:
为什么要实现drawRect:方法才干画图到view上?
由于在drawRect:方法中才干取得跟view相关联的图形上下文
drawRect:方法在什么时候被调用?
当view第一次显示到屏幕上时(被加到UIWindow上显示出来)
调用view的setNeedsDisplay或者setNeedsDisplayInRect:时
Quartz2D画图的代码步骤
//获得图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//拼接路径(以下代码是搞一条线段)
CGContextMoveToPoint(ctx, 10, 10);
CGContextAddLineToPoint(ctx, 100, 100);
//绘制路径
CGContextStrokePath(ctx); // CGContextFillPath(ctx);
Quartz2D经常使用拼接路径函数
//新建一个起点
void CGContextMoveToPoint(CGContextRef c, CGFloat x, CGFloat y)
//加入新的线段到某个点
void CGContextAddLineToPoint(CGContextRef c, CGFloat x, CGFloat y)
//加入一个矩形
void CGContextAddRect(CGContextRef c, CGRect rect)
//加入一个椭圆
void CGContextAddEllipseInRect(CGContextRef context, CGRect rect)
//加入一个圆弧
void CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y,
CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)
//Mode參数决定绘制的模式
void CGContextDrawPath(CGContextRef c, CGPathDrawingMode mode)
//绘制空心路径
void CGContextStrokePath(CGContextRef c)
//绘制实心路径
void CGContextFillPath(CGContextRef c)
//提示:一般以CGContextDraw、CGContextStroke、CGContextFill开头的函数。都是用来绘制路径的
Quartz2D图形上下文栈的操作
//将当前的上下文copy一份,保存到栈顶(那个栈叫做”图形上下文栈”)
void CGContextSaveGState(CGContextRef c)
//将栈顶的上下文出栈,替换掉当前的上下文
void CGContextRestoreGState(CGContextRef c)
Quartz2D基本线条绘制实例
/**
* 在view第一次显示到屏幕上的时候会调用一次
*/
- (void)drawRect:(CGRect)rect {
//1.获得图形上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.拼接图形路径
//设置线段宽度
CGContextSetLineWidth(ctx, 5);
//设置线段头尾部的样式
CGContextSetLineCap(ctx, kCGLineCapRound);
//设置线段转折点的样式
CGContextSetLineJoin(ctx, kCGLineJoinRound);
//第一条线 (设置颜色)
CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);
//设置一个起点
CGContextMoveToPoint(ctx, 10, 10);
//加入一条线段到(100。100)
CGContextAddLineToPoint(ctx, 100, 100);
//渲染
CGContextStrokePath(ctx);
//第二条线
CGContextSetRGBStrokeColor(ctx, 0, 0, 1, 1);
CGContextMoveToPoint(ctx, 30, 30);
CGContextAddLineToPoint(ctx, 140, 40);
CGContextAddLineToPoint(ctx, 180, 150);
CGContextStrokePath(ctx);
//第三条线
CGContextSetRGBStrokeColor(ctx, 0, 1, 1, 1);
CGContextMoveToPoint(ctx, 160, 260);
CGContextAddLineToPoint(ctx, 100, 40);
CGContextAddLineToPoint(ctx, 100, 200);
CGContextAddLineToPoint(ctx, 50, 100);
CGContextSetLineWidth(ctx, 10);
CGContextStrokePath(ctx);
//第四条线
CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);
CGContextMoveToPoint(ctx, 90, 340);
CGContextAddLineToPoint(ctx, 200, 340);
CGContextSetLineWidth(ctx, 2);
CGContextStrokePath(ctx);
}
@end
效果图
Quartz2D形状绘制实例
- (void)drawRect:(CGRect)rect {
drawTriangle();
drawQuadrilateral();
}
/**
* 四边形
*/
void drawQuadrilateral()
{
//1.获得图形上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.画图形
CGContextAddRect(ctx, CGRectMake(50, 150, 150, 200));
[[UIColor orangeColor]set];
//3.绘制图形
//CGContextFillPath(ctx); //填充
CGContextStrokePath(ctx); //非填充
};
/**
* 三角形
*/
void drawTriangle()
{
//1.获得图形上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.画三角形
CGContextMoveToPoint(ctx, 125,20);
CGContextAddLineToPoint(ctx, 10, 120);
CGContextAddLineToPoint(ctx, 250, 120);
//关闭路径
CGContextClosePath(ctx);
CGContextSetLineWidth(ctx, 8);
CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);
//3.绘制图形
CGContextStrokePath(ctx);
}
效果图
- (void)drawRect:(CGRect)rect {
drawCircle();
drawArc();
}
/**
* 画圆弧
*/
void drawArc()
{
// 1.获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.画圆弧
// x/y : 圆心
// radius : 半径
// startAngle : 開始角度
// endAngle : 结束角度
// clockwise : 圆弧的伸展方向(0:顺时针, 1:逆时针)
CGContextAddArc(ctx, 100, 300, 50, M_PI_2, M_PI, 0);
CGContextSetRGBStrokeColor(ctx, 1, 1, 0, 0.8);
// 3.显示绘制
CGContextStrokePath(ctx);
//画1/4圆
CGContextMoveToPoint(ctx, 100, 100);
CGContextAddLineToPoint(ctx, 100, 150);
CGContextAddArc(ctx, 100, 100, 50, -M_PI_2, M_PI, 1);
CGContextClosePath(ctx);
[[UIColor redColor] set];
CGContextFillPath(ctx);
}
/**
* 画圆
*/
void drawCircle()
{
//1.获得上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.画圆
CGContextAddEllipseInRect(ctx, CGRectMake(50, 10, 150, 150));
CGContextSetLineWidth(ctx, 10);
CGContextSetRGBStrokeColor(ctx, 1, 0, 1, 1);
//3.显示绘画
CGContextStrokePath(ctx);
}
效果图
Quartz2D图片、文字绘制实例
/**
* 绘制图片
*/
void drawImage()
{
//1.取得图片
UIImage *image=[UIImage imageNamed:@"square"];
//[image drawAtPoint:CGPointMake(50, 50)];
//[image drawInRect:CGRectMake(0, 0, 150, 150)];
[image drawAsPatternInRect:CGRectMake(10, 10, 240, 240)];
//加入水印
NSString * str=@"作者: W先生";
[str drawInRect:CGRectMake(160, 200, 100, 30) withAttributes:nil];
}
/**
* 绘制文字
*/
void drawText()
{
//1.获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//设置一个背景
CGRect bgRect = CGRectMake(80, 300, 100, 100);
CGContextAddRect(ctx, bgRect);
CGContextFillPath(ctx);
//绘制文字
NSString * str=@"爱编程,爱画图,不解释!!";
NSMutableDictionary *attrs=[NSMutableDictionary dictionary];
//设置文字颜色
attrs[NSForegroundColorAttributeName]=[UIColor orangeColor];
//设置文字字体
attrs[NSFontAttributeName]=[UIFont systemFontOfSize:25];
[str drawInRect:bgRect withAttributes:attrs];
}
效果图
iOS开发 - Quartz2D画图的更多相关文章
- iOS开发-Quartz2D初识
Quartz2D如果单独的从Quartz,那么会发现Quartz是一个开源的Java作业调度框架,单独从英文翻译的角度来看的话Quartz的英文是石英,如果有的时候不小心搜索会发现手表推荐.本文中介绍 ...
- IOS开发之——画图(CGContext)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zhenyu5211314/article/details/24230581 0 CGContext ...
- iOS开发Quartz2D 十三:画板涂鸦
一:效果如图: 二:代码 #import "ViewController.h" #import "DrawView.h" #import "Handl ...
- iOS开发Quartz2D之十二:手势解锁实例
一:效果如图: 二:代码: #import "ClockView.h" @interface ClockView() /** 存放的都是当前选中的按钮 */ @property ( ...
- iOS开发Quartz2D十二:手势解锁实例
一:效果如图: 二:代码: #import "ClockView.h" @interface ClockView() /** 存放的都是当前选中的按钮 */ @property ( ...
- iOS开发Quartz2D之八:图形上下文状态栈
#import "DrawView.h" @implementation DrawView - (void)drawRect:(CGRect)rect { // Drawing c ...
- iOS开发Quartz2D之 七:雪花效果
#import "VCView.h" @implementation VCView -(void)awakeFromNib { //[NSTimer scheduledTimerW ...
- iOS开发Quartz2D 三 进度条的应用
一:效果如图: 二:代码 #import "ViewController.h" #import "ProgressView.h" @interface View ...
- iOS Quartz2D画图
对于刚接触Quartz2D的同学来说,先了解 上下文 的概念,再从最基础的画线来具体体验Quartz2D的画图步骤 介绍Quart2D :是苹果官方的二维(平面)绘图引擎,同时支持iOS和macOS系 ...
随机推荐
- 64。node.js 中间件express-session使用详解
转自:http://jinjiakarl.com/2018/06/09/node-js-%E4%B8%AD%E9%97%B4%E4%BB%B6express-session%E4%BD%BF%E7%9 ...
- CSS的导入方式:link与import方式的区别
在前端开发中,加载CSS样式文件有两种方式:link方式与import方式,它们之间的区别主要有以下几点: 1.兼容性不一样 link是一个HTML标签,所以它不存在兼容性问题,而import方式则具 ...
- Android WebView页面加载优化
目前webapp越来越多,体验也越来越好,为了能够更好的使用WebView展示出流畅的的页面,可以从以下几点做优化: WebView缓存 资源文件本地存储 减少耗时操作 客户端UI优化 可能有人会说了 ...
- C/C++(结构体)
结构体(struct) 从某种意义上说,会不会使用struct,如何使用struct是区别一个开发人员是否具备丰富开发经验的试金石. 处理由不同类型成员构成的构造类型,要采用结构体的方式. 定义:关键 ...
- 【2017 Multi-University Training Contest - Team 5】Rikka with Graph
[Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6090 [Description] 给你n个点; 让你在这n个点上最多连m条无向边; 使得 ∑ni= ...
- ZOJ 3524 Crazy Shopping
Crazy Shopping Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ...
- 玩转阿里云server——安装WebserverTomcat7
1. 以root用户身份登录阿里云server 2. 使用apt-get install安装Tomcat7 sudo apt-get install tomcat7 3.安装后.Tomcat在启动时报 ...
- Pig源代码分析: 简析运行计划的生成
摘要 本文通过跟代码的方式,分析从输入一批Pig-latin到输出物理运行计划(与launcher引擎有关,通常是MR运行计划.也能够是Spark RDD的运行算子)的总体流程. 不会详细涉及AST怎 ...
- Android-CheckBox 实现计算器
源码下载地址:http://download.csdn.net/detail/wu20093346/7718055 使用CheckBox的OnCheckedChangeListener做事件触发,效果 ...
- values-dimen 不同分辨率资源实现引用
今天遇到了一种情况,就是在不同分辨率下面出现了需要设定不同的距离,当时第一反映就是重新定义一个layout.但是,仅仅为了更改一个数值就复制那么多的代码,明显不合里.后来就想到干脆在不同的分辨率下创建 ...