我正在使用UIBezierPath在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 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:myPath, @"Path", brushPattern, @"Colors", nil];
[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];
}
答案 0 :(得分:8)
存储擦除的BOOL值:BOOL _erase;
BOOL eraseButtonIsTapped = ...
if eraseButtonIsTapped {
_erase = yes;
} else{
_erase = NO;
}
绘图时:
[myPath strokeWithBlendMode:_erase?kCGBlendModeClear:kCGBlendModeNormal alpha:1.0f];
答案 1 :(得分:4)
试试这个
brushPattern = view.backgroundColor;
这将绘制一条新线,其颜色正好在绘制路径的后面。并且您可以使用相同的pathArray来执行此操作。因此,稍后您也可以实现重做/撤消操作。如果你愿意我可以解释你更多。