我的CGPathRef
有 5 个元素,我想删除起点,并有一个包含 4 个元素的路径,其起点与我要删除的起点的路径的终点相同。
这是我现在的函数,它生成一个异常:
static void constructPath(void *info, const CGPathElement *element)
{
NSBezierPath *bezierPath = (NSBezierPath *)info;
switch (element->type) {
case kCGPathElementMoveToPoint:
// some kind of skipping using points[0] kCGPathElementMoveToPoint
[bezierPath lineToPoint:element->points[1]]; // line exception in gdb
[bezierPath lineToPoint:element->points[2]];
[bezierPath lineToPoint:element->points[3]];
[bezierPath lineToPoint:element->points[4]];
break;
case kCGPathElementAddLineToPoint:
if ([bezierPath isEmpty]) {
[bezierPath moveToPoint:element->points[0]]; // the new start, how to go to next from here ?
} else {
[bezierPath moveToPoint:element->points[1]];
[bezierPath moveToPoint:element->points[2]];
[bezierPath moveToPoint:element->points[3]];
[bezierPath moveToPoint:element->points[4]];
}
break;
default:
break;
}
}
只需忽略原始路径的第一个元素(类型为 kCGPathElementMoveToPoint
)。然后,为每行添加新行,除非路径为空,在这种情况下,您需要移动到原始点。
static void constructPath(void *info, const CGPathElement *element){ NSBezierPath *bezierPath = (NSBezierPath *)info; if ( kCGPathElementAddLineToPoint == element->type ) { if ( [bezierPath isEmpty] ) { [bezierPath moveToPoint:element->points[0]]; } else { [bezierPath lineToPoint:element->points[0]]; } }}