6

我在 IOS 中绘制应用程序时遇到了一些麻烦。我在一些教程的帮助下创建了自由手绘图。但是我发现在擦除这幅画时有些困难。在我的应用程序中,我有一个带有橡皮擦的按钮作为背景图像。单击橡皮擦按钮后,当我在绘图上滑动时,它会在我滑动的任何地方擦除绘图。谁能帮我做到这一点。提前致谢。

下面是我的代码:

@implementation LinearInterpView
{
    UIBezierPath *path;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder {

    if(self = [super initWithCoder:aDecoder]) {
        [self setMultipleTouchEnabled:YES];
        [self setBackgroundColor:[UIColor whiteColor]];
        path=[UIBezierPath bezierPath];
        [path setLineWidth:2.0];
    }
    return self;
}

-(void)drawRect:(CGRect)rect{
    [[UIColor blackColor] setStroke];
    [path stroke];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[touches anyObject];
    CGPoint p=[touch locationInView:self];
    [path moveToPoint:p];
}

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

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesMoved:touches withEvent:event];
}

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

    [self touchesEnded:touches withEvent:event];
}

// This is the button action to erase the drawing.
- (IBAction)erase:(id)sender {
    CGContextRef cgref=UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(cgref, kCGBlendModeClear);
}

请清除我,我犯了什么错误。

4

4 回答 4

1

因此,通过绘图,您的意思是您已经在屏幕上绘制了一些颜色的线条,您可以通过设置白色和 alpha 1 来做同样的事情,以便白线替换现有的彩色线条。一个更好的教程在这里似乎也很重要。

于 2013-10-04T05:26:31.483 回答
1

首先,您的逻辑应该是在 ImageView 上创建一个图层。

然后您可以在该图层上绘制,然后通过白色擦除。

它看起来像擦除,您的视图将根据要求看起来像。

那肯定会奏效。

于 2013-10-04T07:27:32.290 回答
1

试试这个擦除 iOS 中的绘图:

- (IBAction)eraserPressed:(id)sender { 
    red = 255.0/255.0;
    green = 255.0/255.0;
    blue = 255.0/255.0;
    opacity = 1.0;
}
于 2014-08-02T13:55:40.717 回答
0

为什么不在橡皮擦按钮中实现与在绘图按钮中相同的逻辑。只需将橡皮擦中笔划的默认颜色设置为白色或背景颜色。

于 2013-10-04T05:54:59.343 回答