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



请帮我撤消并转发我的抽奖。我将所有路径存储到pathArray中,现在我想撤消并转发路径(如应用程序注释(Apple((。 请帮我解决这个问题!

这是我的路径数组类

//  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

这是我的绘画课

- (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.

最新更新