Bezier Path Basics
UIBezierPath
类对象是对CGPathRef的封装。建立一个path包括下面几个步骤:
1、创建path的对象
2、确定起始点,通过函数moveToPoint:
3、添加一些由线和弧构成的subpath
4、设置UIBezierPath对象的属性,线段宽度,线段链接方式等
path的点的坐标都是相对于(0,0)的,在绘画的时候,这些点都被会在了当前的context中,相对于当前context的坐标原点,这样移动path的时候,只需要将当前的context设置一个对应的CTM就可以了。冰柜还可以通过当前context的stack来保存和undo当前context的状态。
Adding Lines and Polygons to Your Path
按照之前说过的创建path的方法,创建线的步骤可以通过下面的代码演示:
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:ccp(100,0)];
[path addLineToPoint:ccp(200,40)];
[path addLineToPoint:ccp(160,140)];
[path addLineToPoint:ccp(40,140)];
[path addLineToPoint:ccp(0,40)];
[path closePath];
通过closePath函数完成path的闭合操作。
Adding Arcs to Your Path
创建弧的函数为类函数:bezierPathWithArcCenter:radius:startAngle: endAngle: clockwise:
这里便不再需要通过调用moveToPoint设置startPoint了。
Adding Curves To Your Path
UIBezierPath提供了方法设置曲线(quad curve)和二次曲线(cubic curve)的路径。
quad curve:需要一个控制点,函数addCurveToPoint:controlPoint1:controlPoint2:
cubic curve:需要两个控制点,函数addQuadCurveToPoint:controlPoint:
Creating Oval and Rectangular Paths
椭圆形和举行都是常见的构成path的图形。在创建path的时候可以通过函数bezierPathWithRect:和 bezierPathWithOvalInRect
:来返回一个path。
但是如果在一个已经有了的path上面添加一个椭圆形的时候最简单是通过这样的方法来实现,需要通过Core Graphics的方法来实现,这点可以参考 “Modifying the Path Using Core Graphics Functions.”
而如果是往一个path中添加一个矩形的话,可以通过moveToPoint:addLineToPoint:closePath来实现。
Modifying the Path Using Core Graphics Functions
前面已经说过UIBezierPath是对CGPath的封装,通过UIBezierPath的CGPathRef可以获得与path相关对应的CGPathRef的对象,尽管通过UIBezierPath的方法可以完成很多绘图操作,但是某些时候,直接采用Core Graphics框架的方法操作会更加的简单方便。
1、绘图的时候可以单纯采用Core Graphics框架,或者混合使用两个框架的东西。但是值得说明的是,从某些方面来说单纯采用Core Graphics框架会更加简单些。这个过程可以通过下面的代码体现:
CGMutablePathRef cgpath = CGPathCreateMutable();
CGPathAddEllipseInRect(cgpath,NULL,CGRectMake(0,0,300,100));
CGPathAddEllipseInRect(cgpath,NULL,CGRectMake(20,20,300,300));
UIBezierPath *path = [UIBezierPath bezierPath];
path.CGPath = cgpath;
CGPathRelease(cgpath);
2、绘图的时候采用混合的方式相对麻烦些,首先创建一个bezierPath,在用Core Graphics框架的时候,需要先通过CGPathMutableCopy()方法将UIBezierPath的CGPath对象进行复制,对复制后对象操作之后,将复制厚的对象赋值给之前的bezierPath对象。代码实例如下:
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect
:CGRectMake(0,0,300,100)];
CGPathRef pathRef = path.CGPath;
CGPathRef newPathRef = CGPathMutableCopy(pathRef);
CGPathRelease(pahtRef);
CGPathAddEllipseInRect(newPathRef,NULL,CGRectMake(20,20,300,300));
path.CGPath = newPathRef;
CGPathRelease(newPathRef);
********通过两段代码很容易看出区别。
Rendering The Contents of a Bezier Path Object
stroke:描线
fill:填充
在设置path的描绘的时候有以下几个步骤:
1、通过UIColor的方法设置stroke和fill的color
2、设置path的位置,可以通过修改CTM修改
3、更新path的属性
另外值得注意的是:stroke一定要在fill之后,防止fill的color占据一般的线的宽度
Doing Hit-Detection on a Path
针对点是否落在一个封闭的fill的path内部问题,在UIKit中可以采用containsPoint:方法(UIBezierPath)。但是需要注意的是,这个方法仅限于封闭的path。该方法监测开放path的时候,都是返回NO,真想监测开放path的话,方法就是复制当前path,将copy封闭之后,对copy进行监测。
如果希望监测一个stroked path的点落位问题,就必须采用Core Graphics框架了。方法CGContextPathContainsPoi
nt让我们监测点的落位问题在fill或者stroke的path。
- (BOOL) containsPoint:(CGPoint)point onPath:(UIBezierPath*)path inFillArea:(BOOL)inFill
{
CGContextRef context = UIGraphicsGetCurrentCont
ext();
CGPathRef cgPath = path.CGPath;
BOOL isHit = NO;
CGPathDrawingMode mode = kCGPathStroke;
if (inFill)
{
if(path.usersEvenOddFillRule)
mode = kCGPathEOFill;
else
mode = kCGPathFill;
}
CGContextSaveGState(context);
CGContextAddPath(context,cgPath);
isHit = CGContextPathContainsPoi
nt(context,point,mode);
CGContextRestoreGState(context);
return isHit;
}
按照之前说过的创建path的方法,创建线的步骤可以通过下面的代码演示:
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:ccp(100,0)];
[path addLineToPoint:ccp(200,40)];
[path addLineToPoint:ccp(160,140)];
[path addLineToPoint:ccp(40,140)];
[path addLineToPoint:ccp(0,40)];
[path closePath];
通过closePath函数完成path的闭合操作。
Adding Arcs to Your Path
创建弧的函数为类函数:bezierPathWithArcCenter:radius:startAngle: endAngle: clockwise:
这里便不再需要通过调用moveToPoint设置startPoint了。
Adding Curves To Your Path
UIBezierPath提供了方法设置曲线(quad curve)和二次曲线(cubic curve)的路径。

quad curve:需要一个控制点,函数addCurveToPoint:controlPoint1:controlPoint2:
cubic curve:需要两个控制点,函数addQuadCurveToPoint:controlPoint:
Creating Oval and Rectangular Paths
椭圆形和举行都是常见的构成path的图形。在创建path的时候可以通过函数bezierPathWithRect:和 bezierPathWithOvalInRect
但是如果在一个已经有了的path上面添加一个椭圆形的时候最简单是通过这样的方法来实现,需要通过Core Graphics的方法来实现,这点可以参考 “Modifying the Path Using Core Graphics Functions.”
Modifying the Path Using Core Graphics Functions
CGMutablePathRef cgpath = CGPathCreateMutable();
CGPathAddEllipseInRect(cgpath,NULL,CGRectMake(0,0,300,100));
CGPathAddEllipseInRect(cgpath,NULL,CGRectMake(20,20,300,300));
UIBezierPath *path = [UIBezierPath bezierPath];
path.CGPath = cgpath;
CGPathRelease(cgpath);
2、绘图的时候采用混合的方式相对麻烦些,首先创建一个bezierPath,在用Core Graphics框架的时候,需要先通过CGPathMutableCopy()方法将UIBezierPath的CGPath对象进行复制,对复制后对象操作之后,将复制厚的对象赋值给之前的bezierPath对象。代码实例如下:
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect
CGPathRef pathRef = path.CGPath;
CGPathRef newPathRef = CGPathMutableCopy(pathRef);
CGPathRelease(pahtRef);
CGPathAddEllipseInRect(newPathRef,NULL,CGRectMake(20,20,300,300));
path.CGPath = newPathRef;
CGPathRelease(newPathRef);
Rendering The Contents of a Bezier Path Object
stroke:描线
fill:填充
在设置path的描绘的时候有以下几个步骤:
1、通过UIColor的方法设置stroke和fill的color
2、设置path的位置,可以通过修改CTM修改
3、更新path的属性
另外值得注意的是:stroke一定要在fill之后,防止fill的color占据一般的线的宽度
Doing Hit-Detection on a Path
- (BOOL) containsPoint:(CGPoint)point onPath:(UIBezierPath*)path inFillArea:(BOOL)inFill
{
}