2012-01-25 23 views
0

我正在研究一個iPhone應用程序的繪圖,用戶可以繪製一條線,也可以刪除以及任何身體可以幫助我如何刪除線或繪圖?如何擦除iPhone中的線條畫(CGGraphics)?

爲平局

- (void)drawRect:(CGRect)rect { 

    float newHeight; 
    float newWidth; 

    if (!myDrawing) { 
     myDrawing = [[NSMutableArray alloc] initWithCapacity:0]; 
    } 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    if (myPic != NULL) { 
     float ratio = myPic.size.height/460; 
     if (myPic.size.width/320 > ratio) { 
      ratio = myPic.size.width/320; 
     } 

     newHeight = myPic.size.height/ratio; 
     newWidth = myPic.size.width/ratio; 

     [myPic drawInRect:CGRectMake(0,0,newWidth,newHeight)]; 
    } 
    if ([myDrawing count] > 0) { 
     CGContextSetLineWidth(ctx, 5); 

     for (int i = 0 ; i < [myDrawing count] ; i++) { 
      NSArray *thisArray = [myDrawing objectAtIndex:i]; 

      if ([thisArray count] > 2) { 
       float thisX = [[thisArray objectAtIndex:0] floatValue]; 
       float thisY = [[thisArray objectAtIndex:1] floatValue]; 

       CGContextBeginPath(ctx); 
       CGContextMoveToPoint(ctx, thisX, thisY); 

       for (int j = 2; j < [thisArray count] ; j+=2) { 
        thisX = [[thisArray objectAtIndex:j] floatValue]; 
        thisY = [[thisArray objectAtIndex:j+1] floatValue]; 

        CGContextAddLineToPoint(ctx, thisX,thisY); 
       } 
       CGContextStrokePath(ctx); 
      } 
     } 
    } 
} 

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    [myDrawing addObject:[[NSMutableArray alloc] initWithCapacity:4]]; 

    CGPoint curPoint = [[touches anyObject] locationInView:self]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]]; 
} 

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    CGPoint curPoint = [[touches anyObject] locationInView:self]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]]; 

    [self setNeedsDisplay]; 
} 

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    CGPoint curPoint = [[touches anyObject] locationInView:self]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]]; 
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]]; 

    [self setNeedsDisplay]; 

} 

-(void)cancelDrawing { 

    [myDrawing removeAllObjects]; 
    [self setNeedsDisplay]; 

} 
+0

請發表您的代碼你畫怎麼辦。沒有它,我們不能幫助.. –

+0

當有人提出這個問題,那麼請在這裏寫出原因的專注。 – Mashhadi

+0

好@Jennis讓我發帖 – Mashhadi

回答

3

您可以使用CGContextSetStrokeColorWithColor並設置視圖的背景顏色,而畫線,以便它看起來就像你正在擦除行代碼。

如果你想向用戶提供的撤銷功能,同時繪製比你可以創建爲下面的函數,

-(void)undoDrawing { 

    [myDrawing removeLastObject]; 
    [self setNeedsDisplay]; 

} 

希望這有助於...

+0

謝謝@Jennis,但我已經撤消功能,但感染問題是用戶可以在背景上放置圖像,我的視圖是不是隻有一種顏色,如果你在背景上添加他的照片,那麼這種方法將無法工作。 – Mashhadi

+0

也許你可以設置StrokeColor來清除顏色並繪製?我不知道,但只是一個猜測.. –

+0

哇好主意我會試試這個:) thatnx – Mashhadi