我在我的视图中跟踪触摸并在关联的“画布”层中创建相应的线。这些点被累积到一个 CGPathRef 中,并在 touchesDidEnd 时间存储在一个 NSArray 中。
在 drawLayer 时间,我绘制当前路径和存储路径,如下所示:
// draw the current line:
CGContextAddPath(ctx, path);
CGContextStrokePath(ctx);
NSLog(@"Canvas drawing path %@", path);
// draw the stored lines
for (NSMutableArray *arr in storedPaths) {
CGMutablePathRef aPath = CGPathCreateMutable();
// set up the path with the CGPointObjects
NSLog(@"Canvas drawing stored path");
BOOL inited = NO;
for (CGPointObject *thePt in arr) {
if (inited==NO) {
CGPathMoveToPoint(aPath, NULL, [thePt.x floatValue], [thePt.y floatValue]);
//CGContextMoveToPoint(ctx, [thePt.x floatValue], [thePt.y floatValue]);
inited = YES;
}
else {
CGPathAddLineToPoint(aPath, NULL, [thePt.x floatValue], [thePt.y floatValue]);
//CGContextAddLineToPoint(ctx, [thePt.x floatValue], [thePt.y floatValue]);
}
}
CGContextAddPath(ctx, aPath);
CGContextStrokePath(ctx);
// didn't help connected problem
//CGPathRelease(aPath);
}
这可以按预期工作,只是它将第一行的终点连接到下一行的起点,而不是将它们保留为不接触的单独行。示例:用户绘制了一个 X,但获得了两个端点相连的 X。
CGClosePath 看起来不像我想要的。任何建议将不胜感激。