I am working on a graphical application, I want to make Undo / Redo, for this I save CGPath for touches ending before NSMutableArray, but I donβt understand how I should display CGPaths when I click Cancel
EDIT1:
How I use BezierPaths. So, I first decided to go with a simple approach, just pat this way, without CGPath.
EDIT2: Since my Undo happens in segments (i, e is piecemeal and not the whole path is deleted), I decided to create an array of arrays, so I made the appropriate changes, and now I will draw CGlayer, with the adoption of CGPath
So here "parentUndoArray" is an array of arrays.
So i did it
I have a DrawingPath class that will do the drawing
//DrawingPath.h @interface DrawingPath : NSObject @property (strong, nonatomic) NSString *pathWidth; @property (strong,nonatomic) UIColor *pathColor; @property (strong,nonatomic) UIBezierPath *path; - (void)draw; @end //DrawingPath.m
So now in my DrawingView I do it like this
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { ctr = 0; bufIdx = 0; UITouch *touch = [touches anyObject]; pts[0] = [touch locationInView:self]; isFirstTouchPoint = YES; [m_undoArray removeAllObjects];//On every touches began clear undoArray } -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint p = [touch locationInView:self]; ctr++; pts[ctr] = p; if (ctr == 4) { pts[3] = midPoint(pts[2], pts[4]); for ( int i = 0; i < 4; i++) { pointsBuffer[bufIdx + i] = pts[i]; } bufIdx += 4; dispatch_async(drawingQueue, ^{ self.currentPath = [[DrawingPath alloc] init]; [self.currentPath setPathColor:self.lineColor]; if (bufIdx == 0) return; LineSegment ls[4]; for ( int i = 0; i < bufIdx; i += 4) { if (isFirstTouchPoint) // ................. (3) { ls[0] = (LineSegment){pointsBuffer[0], pointsBuffer[0]}; [self.currentPath.path moveToPoint:ls[0].firstPoint]; // [offsetPath addLineToPoint:ls[0].firstPoint]; isFirstTouchPoint = NO; } else { ls[0] = lastSegmentOfPrev; } float frac1 = self.lineWidth/clamp(len_sq(pointsBuffer[i], pointsBuffer[i+1]), LOWER, UPPER); // ................. (4) float frac2 = self.lineWidth/clamp(len_sq(pointsBuffer[i+1], pointsBuffer[i+2]), LOWER, UPPER); float frac3 = self.lineWidth/clamp(len_sq(pointsBuffer[i+2], pointsBuffer[i+3]), LOWER, UPPER); ls[1] = [self lineSegmentPerpendicularTo:(LineSegment){pointsBuffer[i], pointsBuffer[i+1]} ofRelativeLength:frac1]; // ................. (5) ls[2] = [self lineSegmentPerpendicularTo:(LineSegment){pointsBuffer[i+1], pointsBuffer[i+2]} ofRelativeLength:frac2]; ls[3] = [self lineSegmentPerpendicularTo:(LineSegment){pointsBuffer[i+2], pointsBuffer[i+3]} ofRelativeLength:frac3]; [self.currentPath.path moveToPoint:ls[0].firstPoint]; // ................. (6) [self.currentPath.path addCurveToPoint:ls[3].firstPoint controlPoint1:ls[1].firstPoint controlPoint2:ls[2].firstPoint]; [self.currentPath.path addLineToPoint:ls[3].secondPoint]; [self.currentPath.path addCurveToPoint:ls[0].secondPoint controlPoint1:ls[2].secondPoint controlPoint2:ls[1].secondPoint]; [self.currentPath.path closePath]; lastSegmentOfPrev = ls[3]; // ................. (7) } [m_undoArray addObject:self.currentPath]; EDIT:2 CGPathRef cgPath = self.currentPath.path.CGPath; mutablePath = CGPathCreateMutableCopy(cgPath); dispatch_async(dispatch_get_main_queue(), ^{ bufIdx = 0; [self setNeedsDisplay]; }); }); pts[0] = pts[3]; pts[1] = pts[4]; ctr = 1; } } } } -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [parentUndoArray addObject:m_undoArray]; }
My drawRect method below
EDIT: now my DrawRect has two cases
- (void)drawRect:(CGRect)rect { switch (m_drawStep) { case DRAW: { CGContextRef context = UIGraphicsGetCurrentContext();//Get a reference to current context(The context to draw) CGContextRef layerContext = CGLayerGetContext(self.currentDrawingLayer); CGContextBeginPath(layerContext); CGContextAddPath(layerContext, mutablePath); CGContextSetStrokeColorWithColor(layerContext, self.lineColor.CGColor); CGContextSetFillColorWithColor(layerContext, self.lineColor.CGColor); CGContextSetBlendMode(layerContext,kCGBlendModeNormal); CGContextDrawPath(layerContext, kCGPathFillStroke); // CGPathRelease(mutablePath); CGContextDrawLayerInRect(context,rectSize, self.newDrawingLayer); CGContextDrawLayerInRect(context, self.bounds, self.permanentDrawingLayer); CGContextDrawLayerInRect(context, self.bounds, self.currentDrawingLayer ); } break; case UNDO: { for(int i = 0; i<[m_parentUndoArray count];i++) { NSMutableArray *undoArray = [m_parentUndoArray objectAtIndex:i]; for(int i =0; i<[undoArray count];i++) { DrawingPath *drawPath = [undoArray objectAtIndex:i]; [drawPath draw]; } } } break; [super drawRect:rect]; }
EDIT2: Now the problem I'm currently facing is that even if I draw small paths or large paths, the data in the array array is the same. But infact, the small path should contain a smaller drawingPath object and the large path should contain more drawPath objects in undoArray, which is finally added to the array array called "ParentUndoArray
Here are the screenshots
1.first screen Shot of a drawn line on a stretch without raising a finger

2.After a cancel operation, only a segment of this row is deleted once
