gpt4 book ai didi

objective-c - UIBezierPath 橡皮擦

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:53:36 37 4
gpt4 key购买 nike

我在 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];
}

最佳答案

存储一个用于删除的 BOOL 值:BOOL _erase;

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

绘制时:

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

关于objective-c - UIBezierPath 橡皮擦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11185763/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com