我正在构建第一次绘制的iOS应用程序。现在我能够使用核心图形绘制线条和曲线,但无法取消绘图。我在绘制时保存所有要点并尝试将它们重用于UNDO和REDO,但没有成功。谁能帮忙知道我做错了什么?
非常感谢任何帮助。
这是我的代码
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[tempPathArray removeAllObjects];
UITouch *touch = [touches anyObject];
previousPoint1 = [touch previousLocationInView:self];
previousPoint2 = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
previousPoint2 = previousPoint1;
previousPoint1 = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];
// calculate mid point
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, mid1.x, mid1.y);
CGPathAddQuadCurveToPoint(path, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGRect bounds = CGPathGetBoundingBox(path);
CGPathRelease(path);
drawBox = bounds;
//Pad our values so the bounding box respects our line width
drawBox.origin.x -= self.lineWidth * 2;
drawBox.origin.y -= self.lineWidth * 2;
drawBox.size.width += self.lineWidth * 4;
drawBox.size.height += self.lineWidth * 4;
UIGraphicsBeginImageContext(drawBox.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
curImage = UIGraphicsGetImageFromCurrentImageContext();
[curImage retain];
UIGraphicsEndImageContext();
[tempPathArray addObject:[NSValue valueWithCGRect:drawBox]];
[self setNeedsDisplayInRect:drawBox];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[pathArray addObject:tempPathArray];
NSLog(@"path array count %d", [pathArray count]);
}
- (void)drawRect:(CGRect)rect
{
NSLog(@"draw rect");
[curImage drawAtPoint:CGPointMake(0, 0)];
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
CGContextMoveToPoint(context, mid1.x, mid1.y);
// Use QuadCurve is the key
CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextStrokePath(context);
[super drawRect:rect];
}
-(void)undoButtonClicked
{
KidsPhotoBookAppDelegate *appDelegate = (KidsPhotoBookAppDelegate*)[[UIApplication sharedApplication]delegate];
if([pathArray count]>0){
[bufferArray addObject:[pathArray lastObject]];
[pathArray removeLastObject];
for (NSMutableArray *tempArray in pathArray) {
for (int i = 0; i < [tempArray count]; i++) {
CGRect draw = [[tempArray objectAtIndex:i] CGRectValue];
[self setNeedsDisplayInRect:draw];
}
}
}
}
答案 0 :(得分:0)
试试这个:
我怀疑你会看到它主要起作用 - 第二行重叠第一行的部分将消失,只有那部分没有的部分将保留。
问题在于,您正在设置需求显示在您仍然拥有的绘图部分,而不是您刚刚腾出的部分。这与你应该做的完全相反:你仍然需要重新绘制的部分,因为它们没有被改变,而你腾出的部分确实需要重新绘制,因为它已经改变了。
因此,设置需要在删除之前显示在您要删除的路径区域中。您不需要设置任何其他需求显示。