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

XMG UIBezierPath与CGContextRef

2016-05-08 18:01 507 查看
1.

贝泽尔路径只有当stroke的时候才会添加到上下文当中

如果想要在stroke之前就添加到上下文中的话

// 把路径添加到上下文
// .CGPath
可以UIkit的路径转换成CoreGraphics路径
CGContextAddPath(ctx, path.CGPath);

如果用贝泽尔stroke 的话只认贝泽尔的状态,是不去管上下文的状态

2.

现在存在的问题是如果我设置的是上下文的状态的话。那么以后绘制的内容所有的状态都和现在一样。

我还需要去单独进行设置

3.



绘图的状态包括线宽和颜色

上下文对象可以理解为内存缓冲区,内容如果直接渲染的话效率比较低。先放到上下文上面,然后再进行渲染的话,可以提高效率


可以保存一份上下文的状态‘

然后下一次的时候恢复上下文的状态

UIBezierPath 尽量不要与CGContextRef 混着用,因为贝泽尔只有Stroke的时候才会添加到上下文对象上面。通过上下文如果想描绘之前控制贝泽尔的话,需要将它添加进来

源码

#import "DrawView.h"

@implementation DrawView

// 如果以后用贝瑟尔绘制图形【path stroke】,上下文的状态由贝瑟尔路径状态
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code

// 1.获取上下文
CGContextRef ctx =
UIGraphicsGetCurrentContext();

// 2.描述路径
//
第一根
UIBezierPath *path = [UIBezierPath
bezierPath];

[path moveToPoint:CGPointMake(10,
125)];

[path addLineToPoint:CGPointMake(240,
125)];

//
把路径添加到上下文
// .CGPath
可以UIkit的路径转换成CoreGraphics路径
CGContextAddPath(ctx, path.CGPath);

//
保存一份上下文的状态
CGContextSaveGState(ctx);

//
设置上下文状态
CGContextSetLineWidth(ctx,
10);

[[UIColor
redColor] set];

//
渲染上下文
CGContextStrokePath(ctx);

//
第二根

// 2.描述路径
//
第一根
path = [UIBezierPath
bezierPath];

[path moveToPoint:CGPointMake(125,
10)];

[path addLineToPoint:CGPointMake(125,
240)];

//
把路径添加到上下文
// .CGPath
可以UIkit的路径转换成CoreGraphics路径
CGContextAddPath(ctx, path.CGPath);

//
还原状态
CGContextRestoreGState(ctx);
// // 设置上下文状态
// CGContextSetLineWidth(ctx, 1);
//
// [[UIColor blackColor] set];

//
渲染上下文
CGContextStrokePath(ctx);

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