2

我在 iPad 应用程序中使用 UIBezierPath 进行免费手绘。我想将橡皮擦应用到

但是,我只想擦除其路径中的绘图不能使用路径颜色作为背景颜色,因为背景上有其他元素。

下面是我如何创建我的手绘图:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.opaque = NO;
        lineWidths = 10;
        brushPattern = [UIColor greenColor]; 
        pathArray = [[NSMutableArray alloc]init];
        bufferArray = [[NSMutableArray alloc]init];
        self.multipleTouchEnabled = NO;
    }

    return self;
}

- (void)drawRect:(CGRect)rect {
    for (NSMutableDictionary *dictionary in pathArray) {
        UIBezierPath *_path = [dictionary objectForKey:@"Path"];
        UIColor *_colors = [dictionary objectForKey:@"Colors"];
        [_colors setStroke];
        _path.lineCapStyle = kCGLineCapRound;
        [_path stroke];
    }
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    myPath = [[UIBezierPath alloc]init];
    myPath.lineWidth = lineWidths;
    CGPoint touchPoint = [[touches anyObject] locationInView:self];

    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    [myPath moveToPoint:[mytouch locationInView:self]];
    [myPath addLineToPoint:CGPointMake(touchPoint.x, touchPoint.y)];

    dict = @{@"Path": myPath, @"Colors": brushPattern};
    [pathArray addObject:dict];

    [self setNeedsDisplay];

    [undoManager registerUndoWithTarget:self selector:@selector(undoButtonClicked) object:nil];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *mytouch = [[touches allObjects] objectAtIndex:0];
    [myPath addLineToPoint:[mytouch locationInView:self]];
    [self setNeedsDisplay];
}
4

2 回答 2

8

为擦除存储一个 BOOL 值:BOOL _erase;

BOOL eraseButtonIsTapped = ...
if eraseButtonIsTapped {
    _erase = yes;
} else{
    _erase = NO;
}

绘图时:

[myPath strokeWithBlendMode:_erase?kCGBlendModeClear:kCGBlendModeNormal alpha:1.0f];
于 2014-05-20T07:58:59.537 回答
4

试试这个

    brushPattern = view.backgroundColor;

这将绘制一条新线,其颜色正好在您绘制的路径后面。您可以使用相同的 pathArray 来执行此操作。这样以后您也可以实现重做/撤消操作。如果你愿意,我可以向你解释更多。

于 2012-08-01T10:54:17.223 回答