您的位置:首页 > 产品设计 > UI/UE

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>

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  图形上下文