iOS绘画核心图形撤消/重做

时间:2013-04-11 18:49:03

标签: iphone ios xcode core-graphics

目前我使用核心图形绘制线条。我该如何为此实现undo redo?这是我用来绘制的代码。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    lastTouch = [touch locationInView:foreGround];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    currentTouch = [touch locationInView:foreGround];

    CGFloat brushSize = 20;
    CGColorRef strokeColor = [UIColor blackColor].CGColor;

    UIGraphicsBeginImageContext(scratchView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [canvasView.image drawInRect:CGRectMake(0, 0, canvasView.frame.size.width, canvasView.frame.size.height)];
    CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 50, [[UIColor blackColor]CGColor]);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, brushSize);
    CGContextSetStrokeColorWithColor(context, strokeColor);
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, lastTouch.x, lastTouch.y);
    CGContextAddLineToPoint(context, currentTouch.x, currentTouch.y);
    CGContextStrokePath(context);
    canvasView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    lastTouch = [touch locationInView:foreGround];

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {


}

0 个答案:

没有答案