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

时间:2018-03-23 08:11:12

标签: ios objective-c drawing undo forward

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

这是我的PathArray类

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

这是我的Draw课程

- (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{ }

我需要创建这些功能。

1 个答案:

答案 0 :(得分:0)

我建议如下(这里没有代码):

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

您还可以查看NSUndoManager