gpt4 book ai didi

objective-c - 我如何在 Xcode 中为这条线创建橡皮擦?

转载 作者:行者123 更新时间:2023-11-29 11:18:18 25 4
gpt4 key购买 nike

如何,我有这段代码。当触摸移动时, View 会添加一条线。现在,如果我想为这条线创建一个橡皮擦,我该怎么做?请早点回答我!

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

UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:drawView];

UIGraphicsBeginImageContext(drawView.frame.size);
[drawView.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)];

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushDimension);

const CGFloat *components = CGColorGetComponents([brushColor CGColor]);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0], components[1], components[2], components[3]);

CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());

drawView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

lastPoint = currentPoint;
}

最佳答案

如果您正在寻找一种删除功能,用户可以使用触摸来删除部分线条而不是 RickyTheCoder 提供的撤消功能,您有 2 个选项。

  1. 第一个选项是使用具有相同背景颜色的画笔背景 View ,因此它认为线在它时被删除实际上只是用与背景相同的颜色涂上颜色。

  2. 第二个选项是使用颜色清晰的画笔并设置混合模式清除所以它删除线和背景 View 仍然可见。

    如果(被删除){

    CGContextSetLineWidth(currentContext, 10);

    CGContextSetStrokeColorWithColor(currentContext, [UIColor clearColor].CGColor);

    CGContextSetFillColorWithColor(currentContext, [UIColor clearColor].CGColor);

    CGContextSetBlendMode(currentContext, kCGBlendModeClear);

    CGContextDrawPath(currentContext, kCGPathStroke);

关于objective-c - 我如何在 Xcode 中为这条线创建橡皮擦?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8457437/

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