使用UIKit在Drawing App中撤消按钮

时间:2013-02-07 23:20:20

标签: iphone xcode ios6

我想尝试实现撤销按钮,一切正常并且我能够缓存动作但不知何故以前绘制的线仍然存在,它们应该在每次撤消动作时消失请帮助

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    path = [[UIBezierPath alloc] init];
    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];
    lastPoint = [touch locationInView:self.view];
    CGPoint p = [touch locationInView:self.view];
    [path moveToPoint:p];
    [pathArray addObject:path];
   // NSLog(@"%@",pathArray);
}

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


    mouseSwiped = YES;
    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    CGPoint p = [touch locationInView:self.view];
    [path addLineToPoint:p];

    UIGraphicsBeginImageContext(self.view.frame.size);
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    [self.tempDrawImage setAlpha:opacity];
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;
   [self.view setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.view];
    [path addLineToPoint:p];

    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(self.view.frame.size);
        [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }

    UIGraphicsBeginImageContext(self.mainImage.frame.size);
    [self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity];
    self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
    self.tempDrawImage.image = nil;
    UIGraphicsEndImageContext();
    [self mainImage];
    [self.view setNeedsDisplay];
     [path removeAllPoints];
}

以下撤销功能和patharray计数在每次点击时递减但绘制线仍在那里

-(IBAction)undoButtonClicked:(id)sender
{
    NSLog(@"%s", __FUNCTION__);
    NSLog(@"pathArray count is %i", [pathArray count]);
    if([pathArray count]>0){
        UIBezierPath *_path=[pathArray lastObject];
        [bufferArray addObject:_path];
        [pathArray removeLastObject];
        //self.tempDrawImage.image;
        [self.view  setNeedsDisplay];
    }
}

1 个答案:

答案 0 :(得分:0)

看起来您正在使用tempImage ivar来累积绘图的笔划,但是当您单击撤消时,您不会执行任何操作来擦除已经绘制的线条。我不知道您的绘图要求有多复杂。如果它们很简单,您可以在每个路径上调用pathArray的视图的-drawRect中迭代-stroke(您需要在路径上或在路径中设置lineWidth等当然,每当你画一个笔划时查看)。作为一个简单的优化,您可以计算新/已删除笔划的边界,然后在视图的相应部分上调用setNeedsDisplayInRect:。因为每次触摸移动时你都会更新tempImage,我想这会有很高的开销。