ios开发 UI高级 图形上下文
2016-09-03 20:54
337 查看
DrawView.m
<span style="font-size:14px;">#import "DrawView.h"
@implementation DrawView
- (void)drawRect:(CGRect)rect {
// 获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 描述路径
// 第一根
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10, 150)];
[path addLineToPoint:CGPointMake(200, 150)];
// 把路径添加到上下文
// .CGPath 可以UIkit的路径转换成CoreGraphics路径
CGContextAddPath(ctx, path.CGPath);
// 保存一份上下文的状态
CGContextSaveGState(ctx);
// 设置上下文状态
CGContextSetLineWidth(ctx, 10);
[[UIColor cyanColor] set];
// 渲染上下文
CGContextStrokePath(ctx);
// 描述路径
path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100, 10)];
[path addLineToPoint:CGPointMake(100, 200)];
// 把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
// 还原状态
CGContextRestoreGState(ctx);
// 渲染上下文
CGContextStrokePath(ctx);
}
</span>
运行结果:
<span style="font-size:14px;">#import "DrawView.h"
@implementation DrawView
- (void)drawRect:(CGRect)rect {
// 获取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 描述路径
// 第一根
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10, 150)];
[path addLineToPoint:CGPointMake(200, 150)];
// 把路径添加到上下文
// .CGPath 可以UIkit的路径转换成CoreGraphics路径
CGContextAddPath(ctx, path.CGPath);
// 保存一份上下文的状态
CGContextSaveGState(ctx);
// 设置上下文状态
CGContextSetLineWidth(ctx, 10);
[[UIColor cyanColor] set];
// 渲染上下文
CGContextStrokePath(ctx);
// 描述路径
path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100, 10)];
[path addLineToPoint:CGPointMake(100, 200)];
// 把路径添加到上下文
CGContextAddPath(ctx, path.CGPath);
// 还原状态
CGContextRestoreGState(ctx);
// 渲染上下文
CGContextStrokePath(ctx);
}
</span>
运行结果:
相关文章推荐
- BitMap类型的图形上下文
- IOS_使用CGContextRef实现线性渐变和圆弧遮罩
- iOS开发CoreGraphics核心图形框架之二——深入理解图形上下文
- iOS开发-Quartz 2D知识大全
- iOS中对图片拉伸的四种方式
- 核心绘图——Core Graphics
- 七 iOS之 矩阵操作(图形上下文的平移、缩放、旋转)
- codeforces 484B B. Maximum Value(二分)
- ueditor的使用
- ambari之quicklinks
- Java多线程系列--“JUC集合”10之 ConcurrentLinkedQueue
- Java多线程系列--“JUC集合”09之 LinkedBlockingDeque
- Java多线程系列--“JUC集合”08之 LinkedBlockingQueue
- Java多线程系列--“JUC集合”07之 ArrayBlockingQueue
- Java简单学习GUI
- 000-algorithm-quick_sort
- 利用Vue.js实现拼图游戏
- 习题2-4 子序列的和(subsequence)(java版本)
- break和continue在语句的作用
- Closest Binary Search Tree Value II