2012-03-08 13 views
-3

実際に私は自由な手でイメージを描画するためのサンプルがほとんどなく、アプリケーションにも統合されています。元に戻す/やり直すなどの追加機能が必要です。 ?これについての助けが必要です。私は次のコードを使用しました。iphone:フリーハンドで描画するときにやり直し/元に戻す方法

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    mouseSwiped = YES; 

    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; 
    currentPoint.y -= 20; 


    UIGraphicsBeginImageContext(self.view.frame.size); 
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 

    mouseMoved++; 

    if (mouseMoved == 10) { 
     mouseMoved = 0; 
    } 
} 
+0

可能な複製http://stackoverflow.com/questions/9528062/how-to-undo-when-we-draw-in-iphone-sdk/9528103#9528103 – Vignesh

+0

HTMLタグは使用しないでください。 –

答えて

2

すべてのパスを配列に格納できます。 新しいものを描画するときは、最後に追加して描画します。削除すると最後のものを取り除き、すべてを再描画します。アクションをやり直すことができるように別の配列で削除したままにし、パス配列に新しいパスが追加されたらREDO配列を消去します。 これは私が考えることができる最も単純な方法です。

+0

ありがとうございます。私にサンプルをくれればよいでしょう。 – vinay

+1

説明が必要ですか?パスを 'CGMutablePathRef'として作成し、配列に格納することができます。 – Alexander

5
CGMutablePathRef path; 
    path = CGPathCreateMutable(); 
    - (void)drawRect:(CGRect)rect 
    { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGPathMoveToPoint(path, NULL, previousPoint.x, previousPoint.y); 
    CGPathAddLineToPoint(path, NULL, lastPoint.x, lastPoint.y); 
    CGContextAddPath(context, path); 

    CGContextSetLineWidth(context, 10.0); 
[[UIColor greenColor] setStroke]; 
    CGContextDrawPath(context,kCGPathFillStroke); 
    } 

これで、元に戻す/やり直しをパスのインデックスで実行できます。

+0

どうすれば 'previousPoin'を得ることができますか?あなたの例では? – iPatel

関連する問題