Quartz 2D是一个二维图形绘制引擎,支持iOS环境和Mac OS X环境,Quartz 2D的API可以实现许多功能,如:基于路径的绘图、透明度、阴影、颜色管理、反锯齿、PDF文档生成和PDF元数据访问等等.

Quartz 2D的API是Core Graphics框架的一部分,因此其中的很多数据类型和方法都是以CG开头的.

ViewController.m 文件:

- (void)viewDidLoad {
[super viewDidLoad]; MyView *view = [[MyView alloc] initWithFrame:CGRectMake(0, 120, 375, 300)];
[self.view addSubview:view];
}

MyView.m 文件:

绘制图片

@implementation MyView
- (void)drawRect:(CGRect)rect {
// Drawing code
// 1.使用绘制的方法显示图片
UIImage *image = [UIImage imageNamed:@"1.png"];
// 2.把图片绘制到视图上
[image drawInRect:self.bounds];
}
@end

图形上下文(Graphics Context)-----绘制目标,画布

Graphics Context是一个数据类型(CGContextRef),封装了 Quartz 绘制图像到输出设备的信息.输出设备可以是PDF文件丶Bitmap或者显示在窗口上.Quartz中所有的对象都是绘制到一个Graphics Context中!

在iOS应用程序中,如果要在屏幕上进行绘制,需要定义一个UIView类,并实现它的drawRect:方法.视图的drawRect方法在视图显示在屏幕上及它的内容需要更新时被调用.在调用自定义的drawRect:方法后,视图对象自动配置绘图环境,以便能立即执行绘图操作

Quartz 2D 中默认的坐标系统是: 原点(0,0)在左下角.

沿着X轴从左向右坐标值增大,沿着Y轴从下到上坐标值增大

- (void)drawRect:(CGRect)rect
{
// 1.绘制图片
UIImage *image = [UIImage imageNamed:@"1.png"];
// 2.
// [image drawInRect:rect];
// 01 获取画布对象
CGContextRef context = UIGraphicsGetCurrentContext();
// 坐标变换
CGContextRotateCTM(context, M_PI);
CGContextScaleCTM(context, -1, 1);
CGContextTranslateCTM(context, 0, -self.frame.size.height);
// 02 绘制图片
CGContextDrawImage(context, rect, image.CGImage);
}
@end

不做任何操作绘制出来的图片:

旋转之后的效果:

自定义Label

- (instancetype)initWithFrame:(CGRect)frame
{ self = [super initWithFrame:frame]; if (self) { // 1.初始化设置属性的值
_font = [UIFont systemFontOfSize:16];
_textColor = [UIColor blackColor];
_backgroundColor = [UIColor whiteColor];
// _textAlignment = NSTextAlignmentLeft; }
return self;
} - (void)setText:(NSString *)text
{ if (_text != text) { _text = text;
}
[self setNeedsDisplay];
} - (void)setTextColor:(UIColor *)textColor
{ if (_textColor != textColor) { _textColor = textColor;
}
[self setNeedsDisplay];
} - (void)setBackgroundColor:(UIColor *)backgroundColor
{ if (_backgroundColor != backgroundColor) { _backgroundColor = backgroundColor;
}
[self setNeedsDisplay];
} // 字体属性配置key注视Attributes--http://www.cnblogs.com/qingche/p/3574995.html
- (void)drawRect:(CGRect)rect {
// Drawing code // 1.绘制文本
// 设置字体颜色
[_textColor setFill]; NSDictionary *dic = @{
NSFontAttributeName : _font,
NSForegroundColorAttributeName : _textColor,
NSBackgroundColorAttributeName : _backgroundColor
}; [_text drawInRect:rect withAttributes:dic];
}

效果图:

绘制图形Demo

@implementation MyView
- (void)drawRect:(CGRect)rect
{ // 1.绘制一条直线
// [self drawZLine]; // 2.绘制一个矩形
// [self drawRect]; // 3.绘制一个圆形
// [self drawArc]; // 4.绘制贝塞尔曲线
[self drawQLine];
} // 4.绘制贝塞尔曲线
- (void)drawQLine
{ // 1.获取画布对象
CGContextRef context = UIGraphicsGetCurrentContext();
// 2.设置画笔
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 2);
// 3.设置画笔的起始点
CGContextMoveToPoint(context, 10, 200);
// 4.开始添加曲线路径
CGContextAddCurveToPoint(context, 30, 50, 175 - 30, 50, 175 - 10, 200);
// 5.开始绘制
CGContextDrawPath(context, kCGPathStroke);
} // 3.绘制一个圆形
- (void)drawArc
{ // 1.获取画布
CGContextRef context = UIGraphicsGetCurrentContext();
// 2.设置画笔
CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
CGContextSetLineWidth(context, 4);
// 3.设置填充颜色
CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);
// 4.绘制圆形路径
CGContextAddArc(context, 100, 100, 50, 0, 2*M_PI, 0);
// 5.开始绘制
CGContextDrawPath(context, kCGPathFillStroke);
} // 2.绘制一个矩形
- (void)drawRect
{ // 1.获取画布
CGContextRef context = UIGraphicsGetCurrentContext();
// 2.设置画笔
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 2);
// 3.设置矩形路径
CGContextAddRect(context, CGRectMake(10, 20, 100, 100));
// 4.开始绘制
CGContextDrawPath(context, kCGPathStroke);
} // 1.绘制一条直线
- (void)drawZLine
{ // 1.获取当前视图的绘制画布
CGContextRef context = UIGraphicsGetCurrentContext();
// 2.设置画笔的颜色
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
// 3.设置画笔的宽度
CGContextSetLineWidth(context, 5);
// 4.设置话题的七点位置
CGContextMoveToPoint(context, 20, 20);
// 5.让画笔移动指定位置绘制出一条线
CGContextAddLineToPoint(context, 220, 20);
CGContextAddLineToPoint(context, 20, 50);
CGContextAddLineToPoint(context, 20, 20);
// 设置填充的颜色
CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
// 6.开始绘制
// kCGPathFillStroke 枚举类型,指定绘制视图颜色填充的方式
CGContextDrawPath(context, kCGPathFillStroke);
}

效果图如下:





颜色渐变Demo

- (void)viewDidLoad {

      [super viewDidLoad];

      //颜色渐变
CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame = CGRectMake(20, 100, 300, 400); NSArray *colors = @[
(id)[UIColor cyanColor].CGColor,
(id)[UIColor colorWithRed:0.170 green:0.752 blue:1.000 alpha:1.000].CGColor,
(id)[UIColor colorWithRed:0.211 green:0.332 blue:1.000 alpha:1.000].CGColor
];
gradient.colors = colors;
gradient.startPoint=CGPointMake(0, 0);
gradient.endPoint=CGPointMake(1, 1);
[self.view.layer insertSublayer:gradient atIndex:0];
}

效果图如下:

在平时的开发中使用Quartz 2D来绘图会有意想不到的效果。


关于Quartz 2D绘图的简单使用的更多相关文章

  1. Quartz 2d绘图

    今天看了一下Quartz 2D绘图,我只想说:不要把绘图和动画那些东西当做一个很复杂的东西,其实只要你认真看还是可以理解的.他们并不难.啰嗦了几句,现在直接进入正题: 前提是我们必须新建一个singl ...

  2. iOS基础 - Quartz 2D绘图的基本步骤

    一.使用Quartz 2D绘图的基本步骤 1) 获取上下文context(绘制图形的地方) 2) 设置路径(路径是用来描述形状的) 3)  将路径添加到上下文 4)  设置上下文属性(设置颜色,线宽, ...

  3. iOS基础 - Quartz 2D绘图

    一.Quartz 2D Quartz 2D是一个二维图形绘制引擎,支持iOS环境和Mac OS X环境. Quartz 2D以PDF的规范为基础的图形库,用来绘制二维文字和图形,允许相同的绘图指令在任 ...

  4. Core Graphics框架 利用Quartz 2D绘图

    首先,什么是Core Graphics和Quartz 2D? Core Graphics:是基于Quartz 2D绘图引擎的一个C语言的API绘图框架.它也是iOS开发中最基本的框架(Framewor ...

  5. 1.1 Quartz 2D 绘图

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书”   Quartz2D 绘图主要步骤:   1. 获取[图形上下文]对象 —— (拿到草稿纸 ...

  6. iOS - Quartz 2D 二维绘图

    1.Quartz 2D 简介 Quartz 2D 属于 Core Graphics(所以大多数相关方法的都是以 CG 开头),是 iOS/Mac OSX 提供的在内核之上的强大的 2D 绘图引擎,并且 ...

  7. iOS_Quartz 2D绘图

    目  录: 一.基础知识掌握 二.Quartz 2D绘图基础:CGContextRef实现简单地绘制图形 三.CGContextRef实现文字.图片.基于路径的图形绘制 四.在内存中绘制位图 五.添加 ...

  8. iOS - Quartz 2D 贝塞尔曲线

    1.贝塞尔曲线 贝塞尔曲线(Bézier curve),又称贝兹曲线或贝济埃曲线,是应用于二维图形应用程序的数学曲线.一般的矢量图形软件通过它来精确画出曲线,贝兹曲线由线段与节点组成,节点是可拖动的支 ...

  9. Quartz 2D(常用API函数、绘制图形、点线模式)

    Quzrtz 2D 绘图的核心 API 是 CGContextRef ,它专门用于绘制各种图形. 绘制图形关键是两步: 1.获取 CGContextRef ; 2.调用 CGContextRef 的方 ...

随机推荐

  1. java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.

    1.前几天搭建单元测后,今天用其测试,结果报了这个问题.网上搜索后,刚开始以为原因是  Spring的 依赖版本的问题,我现在的依赖是: 因为其他的比如说 spring-content  spring ...

  2. lsqnonlin函数使用方法

    非线性最小二乘函数  lsqnonlin  格式x = lsqnonlin(fun,x0) %x0 为初始解向量:fun为,i=1,2,-,m,fun返回向量值F,而不是平方和值,平方和隐含在方法中, ...

  3. 使用pdb调试python脚本

    pdb 是 python 自带的一个包,为 python 程序提供了一种交互的源代码调试功能,主要特性包括设置断点.单步调试.进入函数调试.查看当前代码.查看栈片段.动态改变变量的值等.pdb 提供了 ...

  4. 【转】Java中的String,StringBuilder,StringBuffer三者的区别

    https://www.cnblogs.com/su-feng/p/6659064.html 最近在学习Java的时候,遇到了这样一个问题,就是String,StringBuilder以及String ...

  5. Java字符串工具类

    import java.io.ByteArrayOutputStream;import java.io.UnsupportedEncodingException;import java.lang.re ...

  6. js 联动实现日期选择,一般用作生日

    实现目标:年月日三个select 输入框,以及一个hidden的input,通过js获取input的值,如果有值切是日期格式,年月日select为input中的时间.否则为空.年默认区间段为1900年 ...

  7. functions and closures are reference types-函数和闭包是引用类型

    Closures Are Reference Types In the example above, incrementBySeven and incrementByTen are constants ...

  8. 【[USACO12DEC]第一!First!】

    一个串不能成为第一的情况有两种 另外一个单词是它的前缀 在分配字母表大小关系的时候出现了矛盾的情况 第一种很好判断,一旦我们在一个单词没有匹配完之前遇到一个结束标志,那么就说明另外一个单词是它的前缀 ...

  9. 让Git不再难学

    写在前面 在团队做过软件开发的,版本控制必是不可或缺的一项.目前,版本控制主要分为集中式版本控制系统和分布式版本控制系统 ,即大家熟知的SVN和Git.Git是当下最流行的分布式版本控制系统,故,今天 ...

  10. PowerShell交互下的热键