带有CGContext Stroke Path的虚线

时间:2014-08-27 06:35:40

标签: ios objective-c ios7 cgcontext

我正在开发一个应用程序,用户应该可以用手指画线。用户可以启用虚线,但在尝试这样做时我遇到了一个奇怪的问题。如果用户慢慢拖动手指,则会出现一条实线,但在快速执行时会出现破折号。无论什么时候启用,我都希望破折号出现。问题显示在下面的GIF上: Dashes do not appear when dragging slowly

我正在使用UIPanGestureRecognizer收集接触点:

-(void)dragGestureCaptured:(UIPanGestureRecognizer *)gesture
{
    NSValue* touchPoint = [NSValue valueWithCGPoint:[gesture locationInView:self.drawingView]];

    if(gesture.state == UIGestureRecognizerStateBegan)
    {
        [self initializePreviousPointValues:[touchPoint CGPointValue]];
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
        EBLog(@"Dragging ends..");
        return;
    }

    previousPoint2 = previousPoint1;
    previousPoint1 = currentPoint;
    currentPoint = [touchPoint CGPointValue];

    CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
    CGPoint mid2 = midPoint(currentPoint, previousPoint1);

    [self.drawingView addDrawColor:self.currentDrawColor];
    [self.drawingView.controlPoints addObject:[NSValue valueWithCGPoint:previousPoint1]];
    [self.drawingView.points addObject:[NSValue valueWithCGPoint:mid2]];
    [self.drawingView.movePoints addObject:[NSValue valueWithCGPoint:mid1]];

    [self.drawingView setNeedsDisplay];
}

drawingView使用controlsPointspointsmovePoints绘制线条:

- (void)drawLinesInContext:(CGContextRef)context
{    
    for (int i = 0; i < [self.points count]; i++)
    {
        CGPoint movePoint = [[self.movePoints objectAtIndex:i] CGPointValue];
        CGPoint controlPoint = [[self.controlPoints objectAtIndex:i] CGPointValue];
        CGPoint point = [[self.points objectAtIndex:i] CGPointValue];

        CGContextMoveToPoint(context, movePoint.x, movePoint.y);
        CGContextAddQuadCurveToPoint(context, controlPoint.x, controlPoint.y, point.x, point.y);

        NSNumber* colorHash = [self.colorHashes objectAtIndex:i];
        UIColor* col = [self.colorsForHashValue objectForKey:colorHash];

        CGFloat red = 0.0f, blue = 0.0f, green = 0.0f, alpha = 1.0f;

        [col getRed:&red green:&green blue:&blue alpha:&alpha];

        if (alpha == 0.0f)
        {
            penWidth = 15.0f;
            CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
        }
        else
        {
            penWidth = 5.0f;
            CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
            CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, alpha);
        }

        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetLineWidth(context, penWidth);
        CGContextSetShouldAntialias(context, false);
        CGContextSetAllowsAntialiasing(context, false);

        if (self.drawDashedLines)
        {
            CGFloat dashLengths[] = {10.0f, 10.0f};
            CGContextSetLineDash(context, 0.0f, dashLengths, 2);
        }

        CGContextStrokePath(context);
    }
}
drawLinesInContext:中调用

drawRect:

我做错了什么,因为在慢慢拖动时破折号不会出现?

我已经看过Drawing a dashed line with CGContextSetLineDash并尝试了CGPathAddLineToPoint,看看它是否有所作为,但事实并非如此。我遇到了同样的问题。

1 个答案:

答案 0 :(得分:2)

问题是你在每个后续点之间绘制了单独的路径,并且 每条路径都以新的破折号模式开始。如果手指缓慢移动,则绘制 很多非常短的路径。它的路径短于10分(第一个 绘制的短划线长度)然后根本看不到破折号模式。

以下伪代码有望展示如何解决这个问题的想法。而不是

for i = 1 ... N {
    move to point[i-1] 
    curve to point[i]
    set line dash
    stroke path
}

应该是

move to point[0]
for i = 1 ... N {
    curve to point[i]
}
set line dash
stroke path