• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 带有 CGContextStrokePath 的虚线

[复制链接]
菜鸟教程小白 发表于 2022-12-12 11:40:27 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

-(void)dragGestureCapturedUIPanGestureRecognizer *)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)drawLinesInContextCGContextRef)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 看看它是否有所作为,但它没有。我遇到了完全相同的问题。



Best Answer-推荐答案


问题是您在每个后续点之间绘制单独的路径,并且 每条路径都以新的破折号模式开始。如果手指缓慢移动,则绘制 很多非常短的路径。如果路径短于 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

关于ios - 带有 CGContextStrokePath 的虚线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25520388/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap