0

iOSアプリに元に戻す/やり直し機能を追加しようとしています。何本か線を引いて、それぞれを元に戻したいです。全部消すことができますが、それだけでは十分ではありません。CGを使うのは初めてなので、助けていただければ幸いです。

私の.h宣言には次のものが含まれます。

CGPoint lastPoint;
NSMutableArray *pathArray;
UIBezierPath *myPath;

.mで、私は持っています:

- (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カウントは常にゼロです。.:(

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

- (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で処理されています。アドバイス/改善/提案を歓迎します。

ありがとう

4

1 に答える 1

1

[pathArray count]ゼロである理由touchesBeganは、コードのどこにも表示されないためだと思います

pathArray = [[NSMutableArray alloc] init];

したがって、メッセージをnullポインタに送信しています(これは許可されていますが、何もしません)。

では、pathArrayを割り当てていますか?それともnullですか?

于 2012-04-12T15:37:51.687 に答える