xCode - 帮助尝试将撤消/重做功能添加到UIBezierPath

时间:2012-04-12 15:30:29

标签: ios xcode core-graphics

我正在尝试向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中处理的。我欢迎任何建议/改进/建议。

由于

1 个答案:

答案 0 :(得分:1)

我要说[pathArray count]touchesBegan为零的原因是我的代码中没有任何地方可以看到

pathArray = [[NSMutableArray alloc] init];

所以你要向空指针发送消息(允许但不做任何事情)。

你也在分配pathArray?或者它是否为空?