【问题标题】:Objective C: How do I draw lines with alpha in quad curve目标 C:如何在四边形曲线中绘制带有 alpha 的线
【发布时间】:2012-07-16 01:17:07
【问题描述】:

我在使用四边形曲线点时遇到点问题。我想为我的线条设置不透明度,但我也在这里看到了点。 这是我的代码..

CGPoint midPoint(CGPoint p1,CGPoint p2)
{
    return CGPointMake ((p1.x + p2.x) * 0.5,(p1.y + p2.y) * 0.5);
}

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

    previousPoint1 = [touch locationInView:self.view];
    previousPoint2 = [touch locationInView:self.view];
    currentTouch = [touch locationInView:self.view];
}

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

    previousPoint2 = previousPoint1;
    previousPoint1 = currentTouch;
    currentTouch = [touch locationInView:self.view];

    CGPoint mid1 = midPoint(previousPoint2, previousPoint1); 
    CGPoint mid2 = midPoint(currentTouch, previousPoint1);

    UIGraphicsBeginImageContext(CGSizeMake(1024, 768));
    [imgDraw.image drawInRect:CGRectMake(0, 0, 1024, 768)];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(context,kCGLineCapRound);
    CGContextSetLineWidth(context, slider.value);
    CGContextSetBlendMode(context, blendMode);
    CGContextSetRGBStrokeColor(context,red, green, blue, 0.5);
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, mid1.x, mid1.y);
    CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
    CGContextStrokePath(context);

    imgDraw.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsGetCurrentContext();
    endingPoint=currentTouch;
}

任何答案将不胜感激。

【问题讨论】:

  • 我认为问题在于您绘制的每条曲线的末端与下一条曲线的开头重叠。不幸的是,我无法立即想到解决此问题的简单方法。你画的是什么背景?
  • 您能否提供更多有关您要完成的工作的背景信息?乍一看,这比仅使用 UIGestureRecognizer 跟踪触摸并覆盖 drawRect 进行绘图要复杂得多。
  • 我只想在一个空的 UIImageView 中画一条线,可以设置它的不透明度..
  • 您使用 UIImageView 而不是 UIView 的任何原因?
  • 用简单的英语来说,问题是您正在绘制许多重叠的线而不是单个路径。 user1118321 的回答很好:他/她向您展示了如何收集点并绘制整个路径而不是添加单独的线,这应该可以解决您的问题。

标签: iphone objective-c ios xcode


【解决方案1】:

您需要做的是保留用户迄今为止输入的所有点的列表,并始终将所有点重新绘制为同一路径的一部分。

你需要一个数组来存储点。像

CGPoint points[kMaxNumPoints];

而不是previousPoint1/2 等。然后在touchesMoved: 中,您将循环遍历这些点。像这样的:

CGContextBeginPath (context);
CGContextMoveToPoint (context, points [ 0 ].x, point [ 0 ].y);
for (int i = 1; i < currNumPoints; i++) 
{
    // I wasn't sure from your example if you wanted the mid point here instead of the 
    // previous point. But you get the idea.
    CGContextAddQuadCurveToPoint (context, points [ i - 1 ].x, points [ i - 1 ].y, point [ i ].x, point [ i ].y);
}
CGContextStrokePath (context);

【讨论】:

  • 当我尝试实现您给定的代码时,我注释掉了我的 CGContextMoveToPoint 和 CGContextAddQuadCurveToPoint.. 问题是您删除了制作四边形曲线的 2 点.. 很抱歉刚刚得到了逻辑,但我没有'不知道如何使用你给定的代码.. :(
  • 我已经更新了上面的代码,向您展示了我的整个代码。希望这一次对您来说更容易。
猜你喜欢
  • 2017-10-21
  • 2018-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-30
  • 1970-01-01
相关资源
最近更新 更多