【问题标题】:ios paint application overwriting paint strokesios绘画应用程序覆盖绘画笔触
【发布时间】:2012-10-21 06:22:00
【问题描述】:

我正在创建一个绘画应用程序,但如果用一种颜色描画一条线,如果用另一条线描画穿过前一个描边的另一条线,则第一个描边将颜色更改为第二种颜色

这是我用来绘画的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    previousPoint1 = [touch previousLocationInView:self];
    previousPoint2 = [touch previousLocationInView:self];
    currentPoint = [touch locationInView:self];

    [self touchesMoved:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView:self];

    /* check if the point is farther than min dist from previous */
    CGFloat dx = point.x - currentPoint.x;
    CGFloat dy = point.y - currentPoint.y;

    if ((dx * dx + dy * dy) < kPointMinDistanceSquared) {
        return;
    }

    previousPoint2 = previousPoint1;
    previousPoint1 = [touch previousLocationInView:self];
    currentPoint = [touch locationInView:self];

    CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
    CGPoint mid2 = midPoint(currentPoint, previousPoint1);
    CGMutablePathRef subpath = CGPathCreateMutable();
    CGPathMoveToPoint(subpath, NULL, mid1.x, mid1.y);
    CGPathAddQuadCurveToPoint(subpath, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
    CGRect bounds = CGPathGetBoundingBox(subpath);

    CGPathAddPath(path, NULL, subpath);
    CGPathRelease(subpath);

    CGRect drawBox = bounds;
    drawBox.origin.x -= self.lineWidth * 2.0;
    drawBox.origin.y -= self.lineWidth * 2.0;
    drawBox.size.width += self.lineWidth * 4.0;
    drawBox.size.height += self.lineWidth * 4.0;

    [self setNeedsDisplayInRect:drawBox];
}

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextAddPath(context, path);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGContextSetLineWidth(context, self.lineWidth);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);

    CGContextStrokePath(context);


    self.empty = NO;
}

你们中的任何人都知道我做错了什么,或者我怎样才能让每个笔画彼此独立?

非常感谢您的帮助。

【问题讨论】:

  • 看起来您需要在路径上使用 kCGBlend... 选项。
  • 您能说得更具体一点,或者如何使用 kCGBlend 以及在哪里使用吗?

标签: iphone ios ipad drawrect


【解决方案1】:

我认为你需要做的是当你改变颜色时开始一条新的路径。不要只是将新颜色的新子路径添加到具有旧颜色的同一路径中。

您需要为每个路径存储它应该呈现的颜色,然后在 drawRect 方法中循环遍历您的路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-09
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2015-12-21
    • 2011-10-26
    相关资源
    最近更新 更多