如何在 iOS 中撤消多点触控绘图

How to undo multitouch drawing in iOS

请帮我撤销和转发我的抽奖。我将所有路径存储到一个 pathArray 中,现在我想撤消并转发该路径(如 app Notes (Apple) )。 请帮我解决这个问题!

这是我的 PathArray class

//  PathArray.h

#import <Foundation/Foundation.h>

@interface PathArray : NSObject
@property (nonatomic) CGPoint start;
@property (nonatomic) CGPoint end;
@property (nonatomic) UIColor* color;
@property (nonatomic) CGFloat pathWidth;
- (instancetype) initWithStartPoint: (CGPoint)start andEnd: (CGPoint)end andColor: (UIColor *) color andPathWidth: (CGFloat) pathWidth;
@end

这是我的抽奖class

- (void)drawRect:(CGRect)rect
{
    [strokecolor setStroke];
    for (int i=0; i<pathArray.count; i++  ) {
        CGContextRef contex = UIGraphicsGetCurrentContext();
        CGContextSetLineCap(contex, kCGLineCapRound);
        CGContextBeginPath(contex);
        CGContextMoveToPoint(contex, pathArray[i].start.x, pathArray[i].start.y);
        CGContextAddLineToPoint(contex, pathArray[i].end.x, pathArray[i].end.y);
        CGContextSetStrokeColorWithColor(contex, pathArray[i].color.CGColor);
        CGContextSetLineWidth(contex, pathArray[i].pathWidth);
        CGContextStrokePath(contex);
    }
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ 
    UITouch* touch = [touches anyObject];
    if ([touch type] == UITouchTypeStylus)
    {
        if(drawable){
            [self.delegate changeScrollViewInteraction:NO];
            UITouch *touch = [touches anyObject];
            lastPoint = [touch locationInView:self];
        }
    }
    else
    {
        [self.delegate changeScrollViewInteraction:YES];
        return;
    }
    [super touchesBegan: touches withEvent: event];
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch type] == UITouchTypeStylus) //pencil touch
    {
        if(drawable){
            CGPoint newpoint = [touch locationInView:self];
            PathArray *paths = [[PathArray alloc]initWithStartPoint:lastPoint andEnd:newpoint andColor:strokecolor andPathWidth:pathWidth];
            [pathArray addObject:paths];
            lastPoint = newpoint;
            [self setNeedsDisplay];
            NSLog(@"%@",pathArray[0]);
        }
    }
    else
    {
        return;
    }
    [super touchesMoved: touches withEvent: event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
     UITouch *touch = [touches anyObject];   
    if ([touch type] == UITouchTypeStylus && drawable) //pencil touch
    {
        [pathEndPointArray addObject:tempEndPointArray];
    }

    if ([touch type] == UITouchTypeStylus && !drawable)
    {
        [self.delegate CacheLIView_showPopup_message:@"Already cupped!"];
    }
    [self touchesMoved:touches withEvent:event];

}
-(void)undo{
}
-(void)foward{
}
-(void)re_draw_after_rotate{ }

我需要创建这些函数。

我建议如下(此处无代码):

  • 创建第二个数组pathUndoArray
  • 撤消后,从 pathArray 中删除最后一个条目(如果存在)并将其添加(在末尾)到 pathUndoArray,然后重绘。
  • 重做时,从 pathUndoArray 中删除最后一个条目(如果存在)并将其添加(在末尾)到 pathArray,然后重绘。
  • 在新触摸时,您必须删除 pathUndoArray

你也可以看看NSUndoManager