当我慢慢画线时,图中的虚线不起作用

Posted

技术标签:

【中文标题】当我慢慢画线时,图中的虚线不起作用【英文标题】:Dotted line in drawing not works when I draw line slowly 【发布时间】:2014-09-02 16:36:14 【问题描述】:

我正在 ios 中开发绘图功能,我有 3 种线条

飞机 虚线 虚线

问题在于虚线,当我快速绘制时它会很好地绘制,但是当我缓慢地绘制它时它会绘制实线

下面是sn-p的代码,请指导我哪里有问题。

- (void)drawingLayerMoved:(UIPanGestureRecognizer *)recognizer 

    //MOVE START
    if (recognizer.state == UIGestureRecognizerStateBegan) 
        dw_mouseSwiped = NO;
        dw_lastPoint = [recognizer locationInView:self.tempDrawImage];

        UIGraphicsBeginImageContext(self.tempDrawImage.frame.size);
        [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.tempDrawImage.frame.size.width, self.tempDrawImage.frame.size.height)];

    
    //MOVING
    else if (recognizer.state == UIGestureRecognizerStateChanged) 

        dw_mouseSwiped = YES;
        CGPoint currentPoint = [recognizer locationInView:self.tempDrawImage];
        CGContextRef dw_context = UIGraphicsGetCurrentContext();

        if( [dw_brushType  isEqual: DRAWING_DOTTED_LINE] ) 
            CGContextSetLineCap(dw_context, kCGLineCapRound);
        
        else if( [dw_brushType  isEqual: DRAWING_DASHED_LINE] ) 
            CGContextSetLineCap(dw_context, kCGLineCapSquare);
        
        else if( [dw_brushType  isEqual: DRAWING_PLANE_LINE]  ) 
            CGContextSetLineCap(dw_context, kCGLineCapRound);
        

        // ADD FEW SPACES B/W DOTS OF LINE
        if( [dw_brushType  isEqual: DRAWING_DASHED_LINE] || [dw_brushType  isEqual: DRAWING_DOTTED_LINE] ) 
            CGFloat dw_dash[] = 2,dw_brush*3,dw_brush*2,dw_brush;
            CGContextSetLineDash(dw_context, 1, dw_dash, 4);
        

        //BRUSH WIDTH ( we have devided it on 3 )
        CGContextSetLineWidth(dw_context, (dw_brush/3));

        if( [dw_drawingLayerMode isEqualToString:DRAWING_LAYER_MODE_ERASER] )
            //BRUSH CLEAR COLOR
            CGContextSetFillColorWithColor( dw_context, [UIColor clearColor].CGColor );
            //CLEAR DRAWING
            CGContextSetBlendMode(dw_context, kCGBlendModeClear);
         else
            // BRUSH RGB COLOR
            CGContextSetRGBStrokeColor(dw_context, dw_red, dw_green, dw_blue, dw_opacity);
            //NORMAL DRAWING
            CGContextSetBlendMode(dw_context,kCGBlendModeNormal);
        

        CGContextMoveToPoint(dw_context, dw_lastPoint.x, dw_lastPoint.y);
        CGContextAddLineToPoint(dw_context, currentPoint.x, currentPoint.y);
        CGContextStrokePath(dw_context);

        //SAVE CURRENT MOVE INFO IN TEMP IMG
        self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();

        //SAVE CURRENT MOVE POINT AS dw_lastPoint
        dw_lastPoint = currentPoint;

    
    //MOVE END
    else if (recognizer.state == UIGestureRecognizerStateEnded) 
        UIGraphicsEndImageContext();
    

我的问题类似于这个问题,但没有找到解决方案: Drawing a dashed line with CGContextSetLineDash

【问题讨论】:

如果要划线,则为每次更改绘制图像路径的方法将不起作用。您将不得不继续添加到相同的可变路径并重新绘制它。 @DavidRönnqvist 如果你能帮我写代码那就太好了,我搜索了很多但没有找到任何解决方案 【参考方案1】:

我解决了这个问题, 我已经用我的自定义逻辑更新了移动代码,

//MOVING
    else if (recognizer.state == UIGestureRecognizerStateChanged) 

        dw_mouseSwiped = YES;
        CGPoint currentPoint = [recognizer locationInView:self.tempDrawImage];
        BOOL dw_addThisPointInLine  =   YES;


        if( ([dw_brushType  isEqual: DRAWING_DASHED_LINE] || [dw_brushType  isEqual: DRAWING_DOTTED_LINE]) && !([dw_drawingLayerMode isEqualToString:DRAWING_LAYER_MODE_ERASER]) ) 

            CGFloat dw_points_distance  = 0.0;
            dw_points_distance = [self distanceBtwPoints:currentPoint p2:dw_lastPoint];

            if( dw_points_distance < dw_brush)
            dw_addThisPointInLine  =   NO;

            if( !(dw_addThisPointInLine) ) 
                if( dw_points_distance > 30 && dw_brush < 50)
                dw_addThisPointInLine  =   YES;
                else if( dw_points_distance > 40 && dw_brush < 80)
                dw_addThisPointInLine  =   YES;
                else if( dw_points_distance > 50 && dw_brush < 100)
                dw_addThisPointInLine  =   YES;
            
        

        if( dw_addThisPointInLine ) 
             //shif the code of move inside this condition.
        


//move code end

【讨论】:

以上是关于当我慢慢画线时,图中的虚线不起作用的主要内容,如果未能解决你的问题,请参考以下文章

MoPub“onInterstitialFailed”adListener 在离线时不起作用

形状虚线,破折号不起作用

路线存在但不起作用,删除的路线仍然有效 Laravel

图形文本标签在折线图中不起作用 python

拖放(jqyoui-droppable)在 AngularJS 中不起作用

Python - 类型图中的 SWIG 不起作用