【问题标题】:How erase a line draw in iphone(CGGraphics)?如何在iphone(CGGraphics)中擦除线条?
【发布时间】:2012-01-25 06:08:18
【问题描述】:

我正在开发一个绘图的 iPhone 应用程序,用户可以画一条线,也可以删除,所以任何人都可以帮助我如何删除线或绘图?

绘制代码

- (void)drawRect:(CGRect)rect {

    float newHeight;
    float newWidth;

    if (!myDrawing) {
        myDrawing = [[NSMutableArray alloc] initWithCapacity:0];
    }
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    if (myPic != NULL) {
        float ratio = myPic.size.height/460;
        if (myPic.size.width/320 > ratio) {
            ratio = myPic.size.width/320;
        }

        newHeight = myPic.size.height/ratio;
        newWidth = myPic.size.width/ratio;

        [myPic drawInRect:CGRectMake(0,0,newWidth,newHeight)];
    }
    if ([myDrawing count] > 0) {
        CGContextSetLineWidth(ctx, 5);

        for (int i = 0 ; i < [myDrawing count] ; i++) {
            NSArray *thisArray = [myDrawing objectAtIndex:i];

            if ([thisArray count] > 2) {
                float thisX = [[thisArray objectAtIndex:0] floatValue];
                float thisY = [[thisArray objectAtIndex:1] floatValue];

                CGContextBeginPath(ctx);
                CGContextMoveToPoint(ctx, thisX, thisY);

                for (int j = 2; j < [thisArray count] ; j+=2) {
                    thisX = [[thisArray objectAtIndex:j] floatValue];
                    thisY = [[thisArray objectAtIndex:j+1] floatValue];

                    CGContextAddLineToPoint(ctx, thisX,thisY);
                }
                CGContextStrokePath(ctx);
            }
        }
    }
}

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

    [myDrawing addObject:[[NSMutableArray alloc] initWithCapacity:4]];

    CGPoint curPoint = [[touches anyObject] locationInView:self];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];
}

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

    CGPoint curPoint = [[touches anyObject] locationInView:self];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];

    [self setNeedsDisplay];
}

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

    CGPoint curPoint = [[touches anyObject] locationInView:self];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.x]];
    [[myDrawing lastObject] addObject:[NSNumber numberWithFloat:curPoint.y]];

    [self setNeedsDisplay];

}

-(void)cancelDrawing {

    [myDrawing removeAllObjects];
    [self setNeedsDisplay];

}

【问题讨论】:

标签: iphone drawing line cgcontextdrawimage


【解决方案1】:

您可以使用CGContextSetStrokeColorWithColor 并在绘制线条时设置视图的背景颜色,这样看起来就像您正在擦除线条。

如果您想在绘图时为用户提供撤消功能,那么您可以为此创建以下函数,

-(void)undoDrawing {

    [myDrawing removeLastObject];
    [self setNeedsDisplay];

}

希望这会有所帮助...

【讨论】:

  • 谢谢@Jennis,但我已经撤消了功能,但感染问题是用户可以将图像放在背景上,我的视图不仅仅是一种颜色,如果你在背景上添加他的照片,那么这种方法不会工作。
  • 也许你可以设置 StrokeColor 来清除颜色和绘图?我不确定,只是猜测..
  • 哇好主意我会试试这个:) thatnx
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 2012-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多