2012-04-09 56 views
1

我正在使用CoreGraphics来实现对我来说工作正常的自由手绘图,现在我想为此绘图实现撤消功能,以便用户可以清除其最后一个笔画。使用CoreGraphics绘图

这是我工作UITouchesBegin和UITouchesMoved的绘图方法。

-(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(); 

    [self setNeedsDisplayInRect:drawBox]; 
} 

-(void)drawRect:(CGRect)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]; 
} 
+0

http://stackoverflow.com/questions/5741880/iphone-paint-application – 2012-04-09 10:49:59

+0

我试图在问题的代码,但不能这样做成功。 [Here](http://stackoverflow.com/questions/6689600/undo-drawing-in-paint-application) – 2012-04-09 10:50:58

回答

2

有可能实现这种对我的观点2种方式

  1. U可以在NSArray的保存路径,并绘制所有环路的同时,调用drawRect方法,然后同时撤消,除去最后对象,将其添加到缓冲区数组并重新绘制所有数组。

  2. U可以获得一个离线缓冲区画布,您可以在绘制点时创建图像,每次绘制时更新它。这里还需要创建一个点数组,但不是每次重绘。当你撤销时,只需删除最后一个对象,并在数组中绘制点时创建一个新的缓冲区画布。

+0

谢谢Dimple第一个选项引起了我的头脑,但如果你可以详细阐述它,因为我也有添加了UITouchesMoved方法。 – 2012-04-09 13:52:17

相关问题