UIBezierPath的橡皮擦

问题描述 投票:2回答:2

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

但是,我想删除其路径中的图形。我cannot使用路径颜色作为背景色,因为背景上还有其他元素。

下面是我如何创建自由手绘图的方法:

- (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];
}
objective-c ios cocoa-touch ipad uibezierpath
2个回答
8
投票

存储要擦除的布尔值:BOOL _erase;

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

绘制时:

[myPath strokeWithBlendMode:_erase?kCGBlendModeClear:kCGBlendModeNormal alpha:1.0f];

4
投票

只需尝试一下

    brushPattern = view.backgroundColor;

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

© www.soinside.com 2019 - 2024. All rights reserved.