2012-04-12 12 views
0

iOSアプリケーションに元に戻す/やり直し機能を追加しようとしています。私はいくつかの線を引いて、それぞれを元に戻したいと思っています。私はすべてを消去することができますが、それは十分ではありません...私はCGを使うことへの私の最初の進出であるので、本当に助けていただきありがとうございます。xCode - UBezierパスに元に戻す/やり直し機能を追加しようとしています。

マイ.hの宣言は、次のとおりです.Mで

CGPoint lastPoint; 
NSMutableArray *pathArray; 
UIBezierPath *myPath; 

を、私は持っている:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"%s", __FUNCTION__); 
    UITouch *touch = [touches anyObject]; 
    myPath=[[UIBezierPath alloc]init]; 
    lastPoint = [touch locationInView:self.view]; 
    [myPath moveToPoint:lastPoint]; 
    lastPoint.y -= 20; 
    [pathArray addObject:myPath]; 
    NSLog(@"pathArray count is %i", [pathArray count]); 

} 


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"%s", __FUNCTION__); 
    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(), brush); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 

    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    savedImage.image = drawImage.image; 

    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 

} 

touchesBeginの終わりには、pathArrayカウントは常にゼロ..です:(

実装するにはth E私はこのコードを使用しています、元に戻す:ここ

- (void)undoButtonTapped { 
    NSLog(@"%s", __FUNCTION__); 
    NSLog(@"pathArray count is %i", [pathArray count]); 
    if([pathArray count]>0){ 
     UIBezierPath *_path=[pathArray lastObject]; 
     [bufferArray addObject:_path]; 
     [pathArray removeLastObject]; 
     [self.view setNeedsDisplay]; 
    } 

} 

カウントがゼロでも..です

このすべて

はのUIViewControllerに処理されています。私はアドバイス/改善/提案を歓迎する。

おかげ

答えて

1

私は[pathArray count]touchesBeganでゼロであることを理由はどこにもあなたのコードでは、私は

pathArray = [[NSMutableArray alloc] init]; 

だからあなたがしている、ヌルポインタ(にメッセージを送信している見ないことであると言うだろう許可しても何もしません)。

あなたはpathArrayを割り当てていますか?それともヌルですか?

+0

ピーター、それに感謝します。今度は配列のカウントがインクリメントされ、undoを選択するとカウントが減少します。ただし、行は削除されません。その他のアドバイス...? –

+1

@DavidDelMonteパスのセグメントを取り消すことはできません。パスを含むビュー(最後のセグメントが削除された後)のみを再描画できます。 'setNeedsDisplay'を呼び出すことが最初のステップですが、パスが表示されるビューの' drawRect'にコードを追加する必要があります。そこにはパスを再描画する場所があります。 –

+0

私はまだ困惑しています。私は画面全体を元に戻すことができるように見えます。私は小さなコードスニペットを読んでくれて本当に感謝しています。ありがとう! –

関連する問題