2012-07-16 118 views
3

我正在繪製項目,我有一個橡皮擦選項。下面給出的代碼用於當我啓動我的應用程序並繪製一些行並繼續使用橡皮擦。它工作正常,我得到橡皮擦效果。現在第二個場景是我畫了10條線,然後點擊「撤消按鈕」並撤消整個事情,然後我重做整個事情,現在當我點擊「橡皮擦按鈕」並嘗試擦除某些部分,但相反,它會清除整個圖紙。這是我想弄清楚的,但我不明白我哪裏出了問題,所以請朋友幫幫我。橡皮擦不適用於iOS繪圖

以下是我的代碼。

- (void)drawRect:(CGRect)rect 
{ 


     case DRAW: 
    { 
     [m_curImage drawAtPoint:CGPointMake(0, 0)]; 

     CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2); 
     CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1); 
     CGContextRef context = UIGraphicsGetCurrentContext(); 



     [self.layer renderInContext:context]; 
     CGContextMoveToPoint(context, mid1.x, mid1.y); 
     CGContextAddQuadCurveToPoint(context, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y); 
     CGContextSetLineCap(context, kCGLineCapRound); 
     CGContextSetLineWidth(context, self.lineWidth); 
     CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor); 
     CGContextSetAlpha(context, self.lineAlpha); 
     CGContextSetAllowsAntialiasing(context, YES); 
     CGContextStrokePath(context); 
     //   [super drawRect:rect];   
    } 
     break; 



     case ERASE: 
     { 
      [m_curImage drawAtPoint:CGPointMake(0, 0)]; 
      CGContextRef context = UIGraphicsGetCurrentContext();  
      CGContextClearRect(context, rect);     
      CGContextSetBlendMode(context, kCGBlendModeClear); 
      [super drawRect:rect]; 
      break;     

    }  


     case UNDO: 
     { 
      [m_curImage drawInRect:self.bounds]; 
      break; 

     } 

     case REDO: 
     { 
      [m_curImage drawInRect:self.bounds]; 
      break;   
     } 


     default: 
      break; 
    }  
} 

這些是當我點擊撤銷/重做時運行的函數。

-(void)redrawLine 
{ 
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f); 
    [self.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    NSDictionary *lineInfo = [m_lineArray lastObject]; 
    m_curImage = (UIImage*)[lineInfo valueForKey:@"IMAGE"]; 
    UIGraphicsEndImageContext(); 
    [self setNeedsDisplayInRect:self.bounds]; 
} 


-(void)undoButtonClicked 
{ 
    if([m_lineArray count] > 0) 
    { 
     NSMutableArray *line = [m_lineArray lastObject]; 
     [m_bufferArray addObject:line]; 
     [m_lineArray removeLastObject]; 
     [self redrawLine]; 
    } 
    m_drawStep = UNDO; 

} 

-(void)redoButtonClicked 
{ 
    if([m_bufferArray count] > 0) 
    { 
     NSMutableArray *line = [m_bufferArray lastObject]; 
     [m_lineArray addObject:line]; 
     [m_bufferArray removeLastObject];   
     [self redrawLine]; 
    } 
    m_drawStep = REDO; 

} 

請告訴我我是否做得對。

問候,

蘭吉特

回答

4

我已經解決了這個問題,這個問題和以下是代碼完美

case :ERASE 
{ 
CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2); 
      CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1); 

      [m_curImage drawAtPoint:CGPointMake(0, 0)]; 
      CGContextRef context = UIGraphicsGetCurrentContext(); 

      [self.layer renderInContext:context]; 
      CGContextMoveToPoint(context, mid1.x, mid1.y); 
      CGContextAddQuadCurveToPoint(context, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y); 
      CGContextSetLineCap(context, kCGLineCapRound); 


      CGContextSetBlendMode(context, kCGBlendModeClear); 
      CGContextSetLineJoin(context, kCGLineJoinRound); 
      CGContextSetLineWidth(context, self.lineWidth); 
      CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor); 
      CGContextSetShouldAntialias(context, YES); 
      CGContextSetAllowsAntialiasing(context, YES); 
      CGContextSetFlatness(context, 0.1f); 

      CGContextSetAlpha(context, self.lineAlpha); 
      CGContextStrokePath(context); 
} 

Registers Ranjit。

這解決了我的問題,如果有人有同樣的問題,請執行此操作。

+0

我也在研究相同的功能,但這個擦除不適合我,你能幫我嗎。當我在擦除模式下它不畫任何東西沒有清除任何東西..請幫助我 – Raj 2012-12-01 16:18:18

+0

對不起。當我在擦除模式下其繪圖正常,沒有清除任何東西..請幫助我 – Raj 2012-12-01 16:28:40

+1

我修正了這個問題,需要設置圖形的背景色來清除顏色..謝謝你很多解決方法 – Raj 2012-12-01 18:12:16

2

這裏正在發生的事情是,當你刪除整個矩形會得到明確的吶?所以不要使用這種擦除方法畫出與屏幕顏色相同的線條,以便它看起來像窗口正在清除,而且它也很容易嘗試這一點。

例如 如果您的繪圖屏幕是黑色的,然後在黑色的屏幕上繪製觸摸移動線,以便它看起來像清除。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
    lastPoint = [touch locationInView:imagevw]; 
    } 

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:imagevw]; 
    UIGraphicsBeginImageContext(self.imagevw.frame.size); 

    [self.imagevw.image drawInRect:CGRectMake(0, 0, self.imagevw.frame.size.width,  self.imagevw.frame.size.height)]; 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), width); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); // black color 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    imagevw.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

這是代碼繪製在黑色的黑視線...只是試試......它可以幫助你..

+0

你好,但你能告訴我什麼是我的代碼問題嗎?,因爲它不會只撤銷重做和那個時候。 – Ranjit 2012-07-18 07:47:01

+0

我不知道發生了什麼,它可能是因爲你存儲所有的最後一個對象,同時撤消和重做,所以之後,如果你擦除它將清除整個矩陣你存儲爲以前的行動,我認爲...不。對不起 – 2012-07-18 08:17:11

+0

嘿沒問題Sandhya,這很好,無論如何,非常感謝,你省去了我的問題。你有任何其他方式來存儲撤消和重做操作? – Ranjit 2012-07-18 08:23:29