文章标题

来源:互联网 发布:教师教育网络研修 编辑:程序博客网 时间:2024/06/08 08:43

//
// JKView.m
// 上下文栈练习
//
// Created by 孙嘉恺 on 16/8/11.
// Copyright © 2016年 孙嘉恺. All rights reserved.
//

import “JKView.h”

@implementation JKView

//上下文栈 就是保存 最后一次 绘图的状态 (颜色, 样式, 矩阵)
- (void)drawRect:(CGRect)rect {
//1.获取当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//———保存栈的状态———–
CGContextSaveGState(ctx);
//2.绘制路径
//2.画红线
UIBezierPath *redLine = [UIBezierPath bezierPath];
[redLine moveToPoint:CGPointMake(150, 0)];
[redLine addLineToPoint:CGPointMake(150, 300)];
[[UIColor redColor] set];//单独只写 set ,系统根据渲染的模式 自动判断
//添加路径到上下文
CGContextAddPath(ctx, redLine.CGPath);
//3.渲染
CGContextStrokePath(ctx);

//-----------恢复你保存的上下文状态-------------CGContextRestoreGState(ctx);//1.画黑线UIBezierPath *blackLine = [UIBezierPath bezierPath];[blackLine moveToPoint:CGPointMake(0, 150)];[blackLine addLineToPoint:CGPointMake(300, 150)];//添加路径到上下文CGContextAddPath(ctx, blackLine.CGPath);//3.渲染CGContextStrokePath(ctx);

}
//- (void)test
//{
//
// //1.获取当前上下文
//
// CGContextRef ctx = UIGraphicsGetCurrentContext();
//
// //2.绘制路径
//
// //1.画黑线
// UIBezierPath *blackLine = [UIBezierPath bezierPath];
// [blackLine moveToPoint:CGPointMake(0, 150)];
// [blackLine addLineToPoint:CGPointMake(300, 150)];
//
// CGContextAddPath(ctx, blackLine.CGPath);
//
// //3.渲染
// CGContextStrokePath(ctx);
//
// //2.画红线
// UIBezierPath *redLine = [UIBezierPath bezierPath];
// [redLine moveToPoint:CGPointMake(150, 0)];
// [redLine addLineToPoint:CGPointMake(150, 300)];
//
// [[UIColor redColor] set];//单独只写 set ,系统根据渲染的模式 自动判断
//
// CGContextAddPath(ctx, redLine.CGPath);
//
//
// //3.渲染
// CGContextStrokePath(ctx);
//
//
//
//}

@end

0 0
原创粉丝点击