【问题标题】:xCode - Help trying to add an undo/redo function to a UIBezier PathxCode - 帮助尝试将撤消/重做功能添加到 UIBezierPath
【发布时间】:2012-04-12 15:30:28
【问题描述】:

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

谢谢

【问题讨论】:

    标签: ios xcode core-graphics


    【解决方案1】:

    我想说[pathArray count]touchesBegan 中为零的原因是我在您的代码中看不到任何地方

    pathArray = [[NSMutableArray alloc] init];
    

    所以你正在向一个空指针发送消息(这是允许的,但什么都不做)。

    那么你是在分配 pathArray 吗?还是为空??

    【讨论】:

    • 彼得,谢谢。现在数组计数递增,当我选择撤消时,计数递减。但是,该行不会被删除。任何其他建议...?
    • @DavidDelMonte 您无法取消绘制路径的一部分。您只能重绘包含路径的视图(在删除最后一段之后)。调用setNeedsDisplay 是第一步,但您必须将代码添加到drawRect 以获得显示路径的视图。在那里你可以重新绘制路径。
    • 恐怕我还是很难过。我似乎能够“撤消”整个屏幕,或者什么也没有。我真的很感激一个小代码 sn-p 让我开始。谢谢!
    • @DavidDelMonte 好的 .. 如果您查看我在 3 月 22 日编辑 中提出的stackoverflow.com/questions/9468707/… 的问题,我在 GitHub (github.com/JoalahDesigns/PathMove) 上发布了指向源代码的链接这是我的一个测试应用程序,它在视图的drawRect 中绘制贝塞尔路径。我正在构建与您不同的路径,但您将能够看到重绘代码的大致位置(参见 JdGraphicView 类)以及“setNeedsDisplay”的使用。您应该能够下载代码并构建应用程序。
    • 谢谢彼得,你帮了我很多忙!我还有一个问题要问,但我会单独做。
    猜你喜欢
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    相关资源
    最近更新 更多