#import "MyView.h"
@implementation MyView
- (void)drawRect:(CGRect)rect
{
//图形上下文
CGContextRef text = UIGraphicsGetCurrentContext();
//第一条路线
//保存上下文的状态
CGContextSaveGState(text);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointZero];
[path addLineToPoint:CGPointMake(100, 100)];
//设置显示样式
CGContextSetLineWidth(text, 20.0);
[[UIColor redColor] set];
//将路径添加到图形上下文
CGContextAddPath(text, path.CGPath);
//渲染
CGContextStrokePath(text);
//第二条路线
UIBezierPath *path2 = [UIBezierPath bezierPath];
[path2 moveToPoint:CGPointMake(200, 200)];
[path2 addLineToPoint:CGPointMake(300, 300)];
//和下面两行的代码效果一样,清除原来的状态
CGContextRestoreGState(text);
// CGContextSetLineWidth(text, 1);
// [[UIColor blackColor] set];
CGContextAddPath(text, path2.CGPath);
//渲染
CGContextStrokePath(text);
}
@end
转载于:https://my.oschina.net/cgphp/blog/418609