#import "DrawView.h"

@implementation DrawView

- (void)drawRect:(CGRect)rect {
// Drawing code //1.获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.描述路径
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(, )];
[path addLineToPoint:CGPointMake(, )]; //3.把路径添加到上下文当中.
CGContextAddPath(ctx, path.CGPath); //保存当前上下文的状态
CGContextSaveGState(ctx); //设置上下文的状态
CGContextSetLineWidth(ctx, );
[[UIColor redColor] set]; CGContextSaveGState(ctx); //4.把上下文当中的内容渲染View
CGContextStrokePath(ctx); UIBezierPath *path2 = [UIBezierPath bezierPath];
[path2 moveToPoint:CGPointMake(, )];
[path2 addLineToPoint:CGPointMake(, )];
//把路径添加到上下文当中.
CGContextAddPath(ctx, path2.CGPath); // CGContextSetLineWidth(ctx, 1);
// [[UIColor blackColor] set];
//从上下文状态栈当中恢复上下文的状态
CGContextRestoreGState(ctx);
CGContextRestoreGState(ctx); //把上下文当中的内容渲染View
CGContextStrokePath(ctx); } @end

上下文状态栈为内存中的一块区域,它用来保存前上下文当的状态.

我们获取的图层上下文当中其实两块区域,一个是存放添加的路径,一个是用来保存用户设置的状态,

这些状态包括线条的颜色,线宽等.

当我们把上下文的内容渲染到View上面的时候,

它会自动将设置的所有上下文状态运行到保存的路径上面显示到View上面.

如果想要有多种状态,可以先把路径渲染到View上面,

再从新添加路径.添加完路径之后,重新设置上下文的状态.

再把新设置的上下文状态渲染到View上面.

我们可以利用上下文状态栈的方式,在设置状态之前,把之前的状态保存到上下文状态栈里面.

下一次想要再使用之前的状态时, 可以从上下文状态当中取出之前保存的上下文状态.上下文状态栈是先进后出的

1.如何把上下文状态保存到上下文状态栈?

CGContextSaveGState(ctx);

2.如何从上下文状态栈中取出上下文状态?

CGContextRestoreGState(ctx);

iOS开发Quartz2D之八:图形上下文状态栈的更多相关文章

  1. iOS开发-Quartz2D初识

    Quartz2D如果单独的从Quartz,那么会发现Quartz是一个开源的Java作业调度框架,单独从英文翻译的角度来看的话Quartz的英文是石英,如果有的时候不小心搜索会发现手表推荐.本文中介绍 ...

  2. iOS开发 - Quartz2D画图

    Quartz 2D简单介绍 是一个二维画图引擎,同一时候支持iOS和Mac系统 Quartz 2D能完毕的工作 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成 ...

  3. 【iOS】Quartz2D基本图形

    一.画线段 - (void)drawRect:(CGRect)rect { // Drawing code // 1.获得图形上下文 CGContextRef ctx = UIGraphicsGetC ...

  4. iOS开发CoreGraphics核心图形框架之一——CGPath的应用

    一.引言    CoreGraphics核心图形框架相较于UIKit框架更加偏于底层.在Objective-C工程中,CoreGraphics其中方法都是采用C语言风格进行编写的,同时其并不支持Obj ...

  5. iOS开发Quartz2D之十二:手势解锁实例

    一:效果如图: 二:代码: #import "ClockView.h" @interface ClockView() /** 存放的都是当前选中的按钮 */ @property ( ...

  6. iOS开发Quartz2D十二:手势解锁实例

    一:效果如图: 二:代码: #import "ClockView.h" @interface ClockView() /** 存放的都是当前选中的按钮 */ @property ( ...

  7. iOS开发Quartz2D 十三:画板涂鸦

    一:效果如图: 二:代码 #import "ViewController.h" #import "DrawView.h" #import "Handl ...

  8. iOS开发Quartz2D 三 进度条的应用

    一:效果如图: 二:代码 #import "ViewController.h" #import "ProgressView.h" @interface View ...

  9. IOS开发之画图形

    1 画线 2 画线第二个方法  相对方法1简洁 3 矩形 4 圆 5 弧线 6画文字(略) 7 画图片(略)

随机推荐

  1. [Angular] How to get Store state in ngrx Effect

    For example, what you want to do is navgiate from current item to next or previous item. In your com ...

  2. 从“窃听门”事件解读手机Rootkit攻击

    从"窃听门"事件解读手机Rootkit攻击 在今年五月讲述了手机流氓软件危害与防治(http://chenguang.blog.51cto.com/350944/557191)文章 ...

  3. uiautomator——第一个例子:打开浏览器,输入网址

    1.在sdk安装目录:E:\Test_Tools\auto_test\app\adt-bundle-windows-x86-20131030\sdk\tools下启动uiautomatorviewer ...

  4. Atcoder At Beginner Contest 068 C - Cat Snuke and a Voyage

    C - Cat Snuke and a Voyage Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem State ...

  5. ubuntu14.04 printk()默认打印的位置

    tail /var/log/syslog 即可显示printk打印的信息

  6. Ubuntu系统简介

    1.ubuntu 远程连接 需要开启ssh 服务 sudo apt-get install openssh-server service ssh start|stop|restart 2.查看Linu ...

  7. jsp 用shiro 的判断 是否有菜单查看的权限

    实例:spring-shiro.xml 123 /admin/repairType/index = roles["ROLE_ADMIN"]/admin/user=roles[&qu ...

  8. 洛谷——P1816 忠诚

    https://www.luogu.org/problem/show?pid=1816#sub 题目描述 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天记 ...

  9. 腾讯2016实习生面试经验(已经拿到offer)

      忐忑了好几天,今天最终收到深圳总部的电话.允许录用我为2016年实习生,感觉整个天空都放晴了.坐标:武汉大学,给大家说说我的面试经历吧,我投的是软件开发--应用开发方向. 一.校招流程 投递简历- ...

  10. caffe 在 windows 下的配置(scripts\build_win.cmd)

    官网配置文档见:GitHub - BVLC/caffe at windows 1. windows 设置 requirements: visual studio 2013/2015 CMake > ...