作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的 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/
我是一名优秀的程序员,十分优秀!