我正在尝试创建一个应用程序,允许用户用手指在iPad上绘制虚线。我有所有的绘图程序来创建单行。我的问题是我应该如何绘制双线,使其在创建曲线时不会重叠。我一直试图通过向我的Path添加第二行来实现此目的,其中偏移量为当前点的x和y。只要您垂直或水平移动,这都可以正常工作。当您以某个角度绘制时,双线会变成一条线。
我保留了一系列存储为NSValues(pathPoints)的点,以便我可以将它们存储起来供以后使用。起点是pathPoints中的第一个点。
UIGraphicsBeginImageContext(self.frame.size);
//This needs to be a dotted line
CGFloat pattern[] = {10,20};
CGContextSetLineDash(UIGraphicsGetCurrentContext(), 3, pattern, 2);
CGMutablePathRef path = CGPathCreateMutable();
CGMutablePathRef path2 = CGPathCreateMutable();
CGPoint startingPoint = [[pathPoints objectAtIndex:0] CGPointValue];
CGPathMoveToPoint(path, nil, startingPoint.x, startingPoint.y);
CGPathMoveToPoint(path2, nil, startingPoint.x + 10, startingPoint.y + 10);
for (int i = 1; i < [pathPoints count]; i++)
{
CGPoint linePoint = [[pathPoints objectAtIndex:i] CGPointValue];
CGPathAddLineToPoint(path, nil, linePoint.x, linePoint.y);
CGPathAddLineToPoint(path2, nil, linePoint.x + 10, linePoint.y + 10);
}
//当然稍后我会将路径添加到上下文并对其进行描边。
CGContextAddPath(UIGraphicsGetCurrentContext(), path);
CGContextAddPath(UIGraphicsGetCurrentContext(), path2);
CGContextStrokePath(UIGraphicsGetCurrentContext());
UIGraphicsEndImageContext();
我非常感谢我能得到的任何帮助。
答案 0 :(得分:2)
如果您对最后加入的线条感到满意(请参见下图),则可以使用CGContextReplacePathWithStrokedPath()
创建效果。
/**
* CGContextRef context = <the context you're drawing to>
* CGFloat lineWidth = <the width of each of your two lines>
* CGFloat lineSpace = <the space between the two lines>
**/
// Create your path here (same as you would for a single line)
CGContextSetLineWidth(context, lineWidth + lineSpace);
CGContextReplacePathWithStrokedPath(context);
CGContextSetLineWidth(context, lineWidth);
CGContextStrokePath(context);