目标C:如何在四边形曲线中绘制带有alpha的线条

时间:2012-07-16 01:17:08

标签: iphone objective-c ios xcode

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

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;
}

任何答案都将不胜感激。

1 个答案:

答案 0 :(得分: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);