构造路径
通常,一系列点组合一起构成一个形状,而若干个形状组合在一起可以构造一个路径,路径可以规则也可以不规则,随意组合。通过Core Graphics很容易管理路径。
主要用到的函数或方法:
CGPathCreateMutable() 创建可变路径的方法
CGContextAddPath 将路径加入到上下文中
CGContextDrawPath绘制路径
CGPathRelease 路径也需要释放
用路径画一组对角线
- (void)drawRect:(CGRect)rect
{
// Drawing code
[self drawTwoLines];
}
- (void)drawTwoLines
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, screenRect.origin.x, screenRect.origin.y);
CGPathAddLineToPoint(path, NULL, screenRect.size.width, screenRect.size.height);
CGPathMoveToPoint(path, NULL, screenRect.size.width, screenRect.origin.y);
CGPathAddLineToPoint(path, NULL, screenRect.origin.x, screenRect.size.height);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context, path);
CGContextSetLineWidth(context, 1.0f);
[[UIColor blueColor] setStroke]