2014-09-02 24 views
6

我開發的IOS繪圖功能,我有3類線虛線不工作時,我畫線慢慢

  • PLANE
  • DASHED
  • DOTTED

的的問題是用DOTTED行,當我畫得很快的時候畫得很好,但是當我慢慢畫出來的時候它畫的是實線

Bellow是代碼片段,請指導我問題出在哪裏。

- (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(); 
    } 
} 

我的問題是這樣的問題simmiler,但沒有找到解決方案吧: Drawing a dashed line with CGContextSetLineDashenter image description here

+0

如果您想劃線,則爲每個更改繪製路徑到圖像的方法將不起作用。你將不得不添加到相同的可變路徑並重繪它。 – 2014-09-02 18:46:52

+0

@DavidRönnqvist如果你可以幫我編寫代碼,我會很好,但我沒有找到任何解決方案 – 2014-09-03 06:51:17

回答

3

我解決了這個問題, 我已經更新移動的代碼與我的定製邏輯,

//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