获取UIBezierPath的所有顶点

时间:2014-02-06 08:05:57

标签: ios objective-c uibezierpath

我有一个由6个顶点组成的UIBezierPath。现在我想从UIBezierPath获取这些顶点。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:3)

您可以使用CGPathApplyCGPathApplierFunction执行此操作。

NSMutableArray *thePoints = [[NSMutableArray alloc] init];
UIBezierPath *aPath;
CGPath theCGPath = aPath.CGPath;
CGPathApply(theCGPath, thePoints, MyCGPathApplierFunc);

它的作用是将Applier函数传递给每个路径的元素。您可以从函数中的每个元素中获取一个点列表,并将它们添加到thePoints

您的申请人功能看起来像

void MyCGPathApplierFunc (void *info, const CGPathElement *element) {
    NSMutableArray *thePoints = (NSMutableArray *)info;

    CGPoint *points = element->points;
    [thePoints addObject:[NSValue valueWithCGPoint:points[0]]]; 
    \\Only 1 point assuming it is a line 
    \\Curves may have more than one point in points array. Handle accordingly.

}