gpt4 book ai didi

ios - 如何在未端到端连接的 quartz 上下文中绘制线条?

转载 作者:行者123 更新时间:2023-11-28 17:33:14 24 4
gpt4 key购买 nike

我在我的 View 中跟踪触摸并在关联的“ Canvas ”中创建相应的线层。这些点被累积到 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 看起来不像我想要的。任何建议将不胜感激。

最佳答案

如果每个偶数/奇数对都是一条线,则将“inited==NO”替换为计数器并使用“counter%2 == 0”,这样对于偶数点,它会移动到您想要的位置,对于它连接前一个点的奇数点。不使用快速枚举而是使用老式的 for 循环可能更容易。

    for (int i = 0; i < [arr count]; ++i) {
CGPointObject *thePt = [arr objectAtIndex:i];
if (i%2 == 0)
CGPathMoveToPoint(aPath, NULL, [thePt.x floatValue], [thePt.y floatValue]);
else
CGPathAddLineToPoint(aPath, NULL, [thePt.x floatValue], [thePt.y floatValue]);
}

关于ios - 如何在未端到端连接的 quartz 上下文中绘制线条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10624540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com