圖形上下文狀態(tài)棧
- 在圖形繪制的過程中搜吧,通常需要繪制多條線,很有可能多條線的顏色、粗細(xì)等會(huì)不同肛冶,那么就需要在第二條路徑對(duì)這些屬性進(jìn)行覆蓋
- 這個(gè)時(shí)候就可以使用圖形上下文狀態(tài)棧
// 1.獲取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 2.描述路徑
// 第一根
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10, 125)];
[path addLineToPoint:CGPointMake(240, 125)];
// 把路徑添加到上下文
// .CGPath 可以UIkit的路徑轉(zhuǎn)換成CoreGraphics路徑
CGContextAddPath(ctx, path.CGPath);
// 保存一份上下文的狀態(tài)
CGContextSaveGState(ctx);
// 設(shè)置上下文狀態(tài)
CGContextSetLineWidth(ctx, 10);
[[UIColor redColor] set];
// 渲染上下文
CGContextStrokePath(ctx);
// 第二根
// 2.描述路徑
path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(125, 10)];
[path addLineToPoint:CGPointMake(125, 240)];
// 把路徑添加到上下文
// .CGPath 可以UIkit的路徑轉(zhuǎn)換成CoreGraphics路徑
CGContextAddPath(ctx, path.CGPath);
// 還原狀態(tài)
CGContextRestoreGState(ctx);
// 渲染上下文
CGContextStrokePath(ctx);
注意,使用這種方法渲染上下文的扯键,設(shè)置的狀態(tài)只能設(shè)置在上下文上面睦袖,不能將狀態(tài)設(shè)置到貝塞爾路徑上面
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者