3

初めて描画するためのiOSアプリを構築しています。コア グラフィックスを使用して線と曲線を描画できるようになりましたが、描画を元に戻すことができません。描画時にすべてのポイントを保存し、それらを UNDO と REDO に再利用しようとしていますが、成功しません。誰が私が間違っているのかを知るのを助けることができますか?

どんな助けでも大歓迎です。

これが私のコードです

   -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [tempPathArray removeAllObjects];
    UITouch *touch = [touches anyObject];

    previousPoint1 = [touch previousLocationInView:self];
    previousPoint2 = [touch previousLocationInView:self];
    currentPoint = [touch locationInView:self];

}

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

    UITouch *touch  = [touches anyObject];

    previousPoint2  = previousPoint1;
    previousPoint1  = [touch previousLocationInView:self];
    currentPoint    = [touch locationInView:self];


    // calculate mid point
    CGPoint mid1    = midPoint(previousPoint1, previousPoint2); 
    CGPoint mid2    = midPoint(currentPoint, previousPoint1);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, mid1.x, mid1.y);
    CGPathAddQuadCurveToPoint(path, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
    CGRect bounds = CGPathGetBoundingBox(path);
    CGPathRelease(path);

    drawBox = bounds;

    //Pad our values so the bounding box respects our line width
    drawBox.origin.x        -= self.lineWidth * 2;
    drawBox.origin.y        -= self.lineWidth * 2;
    drawBox.size.width      += self.lineWidth * 4;
    drawBox.size.height     += self.lineWidth * 4;

    UIGraphicsBeginImageContext(drawBox.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    curImage = UIGraphicsGetImageFromCurrentImageContext();
    [curImage retain];
    UIGraphicsEndImageContext();

    [tempPathArray addObject:[NSValue valueWithCGRect:drawBox]];


    [self setNeedsDisplayInRect:drawBox];

}

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

    [pathArray addObject:tempPathArray];
    NSLog(@"path array count %d", [pathArray count]);
}

    - (void)drawRect:(CGRect)rect
{
    NSLog(@"draw rect");
    [curImage drawAtPoint:CGPointMake(0, 0)];
    CGPoint mid1 = midPoint(previousPoint1, previousPoint2); 
    CGPoint mid2 = midPoint(currentPoint, previousPoint1);

    context = UIGraphicsGetCurrentContext(); 

    [self.layer renderInContext:context];

    CGContextMoveToPoint(context, mid1.x, mid1.y);
    // Use QuadCurve is the key
    CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y); 

    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, self.lineWidth);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);

    CGContextStrokePath(context);
    [super drawRect:rect];

}

    -(void)undoButtonClicked
{

    KidsPhotoBookAppDelegate *appDelegate = (KidsPhotoBookAppDelegate*)[[UIApplication sharedApplication]delegate];


    if([pathArray count]>0){
        [bufferArray addObject:[pathArray lastObject]];
        [pathArray removeLastObject];

        for (NSMutableArray *tempArray in pathArray) {
            for (int i  = 0; i < [tempArray count]; i++) {
                CGRect draw = [[tempArray objectAtIndex:i] CGRectValue];
                [self setNeedsDisplayInRect:draw];
            }
        }
    }


}
4

1 に答える 1

0

これを試して:

  1. 線を引きます。
  2. 最初の線の上に別の線を引きます。
  3. 元に戻す。

ほとんどの場合、最初の行と重なった 2 番目の行の部分が消え、重なっていない部分だけが残ります。

問題は、まだ残っている図面の部分に必要な表示を設定していることですが、空にした部分ではありません。これはまさにあなたがやるべきことの逆です: まだ残っている部分は変更されていないので再描画する必要はありませんが、空にした部分は変更されているので再描画する必要があります.

そのため、削除する直前に、削除するパスの領域に表示を設定する必要があります。他のもののニーズ表示を設定する必要はありません。

于 2012-05-07T23:27:22.893 に答える