2012-01-25 15 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

ok @Jennis投稿する – Mashhadi

答えて

3

あなたが行を消去されているように見えますので、あなたがCGContextSetStrokeColorWithColorを使用してラインを描きながら、あなたのビューの背景の色を設定することができますを描くための

コード。

そして、あなたはそのために次の関数を作成することができるよりも描きながら、このことができます

-(void)undoDrawing { 

    [myDrawing removeLastObject]; 
    [self setNeedsDisplay]; 

} 

希望を...ユーザーアンドゥ機能を提供する場合

+0

Thanks @Jennisしかし、私はすでに機能を元に戻していますが、感染の問題はユーザーが背景に画像を置くことができるということです。あなたの写真をバックグラウンドで追加すれば、このアプローチはうまくいかないでしょう。 – Mashhadi

+0

ストロークカラーをクリアして描画するように設定できますか?私は確信していませんが、ちょうど推測です。 –

+0

うわー私はこれを試してみましょう:) thatnx – Mashhadi

関連する問題