Iphone 如何使用alpha绘制直线<;1英寸石英2d

Iphone 如何使用alpha绘制直线<;1英寸石英2d,iphone,ios,ios4,quartz-graphics,Iphone,Ios,Ios4,Quartz Graphics,毫无疑问,这可能是一个重复的问题,但我无法从这里的任何帖子得到正确的解决方案。因此,我把这篇文章作为一篇新文章发表,希望能得到一些解决方案 我在我的绘图应用程序我想给一个不透明的特点,为线绘制。 但当我将alpha指定给颜色时,我得到的是线之间的重叠点 我发现在两个视图中绘制解决了这个问题,但我无法理解其背后的逻辑 我用普通的石英2d代码在触摸事件上画线 看起来您在一系列触摸事件的每个点上都画了一个圆圈。相反,您可以使用CoreGraphics/Quartz2D构建一个应用程序,将线宽设置为任

毫无疑问,这可能是一个重复的问题,但我无法从这里的任何帖子得到正确的解决方案。因此,我把这篇文章作为一篇新文章发表,希望能得到一些解决方案

我在我的绘图应用程序我想给一个不透明的特点,为线绘制。 但当我将alpha指定给颜色时,我得到的是线之间的重叠点

我发现在两个视图中绘制解决了这个问题,但我无法理解其背后的逻辑


我用普通的石英2d代码在触摸事件上画线

看起来您在一系列触摸事件的每个点上都画了一个圆圈。相反,您可以使用
CoreGraphics/Quartz2D
构建一个应用程序,将线宽设置为任意厚度,然后根据需要绘制该路径,以保持UI美观。我已经有一段时间没有这样做了,但我认为您需要的大部分内容都是在
CoreGraphics/CGContext.h
~/CGPath.h中,等等。
查看我对另一个CoreGraphics问题的答案

我现在还不知道的一点是,在使用
CGPathCloseSubpath()
将一个CGMutablePathRef“关闭”到一个上下文之前,是否可以将其划入上下文。你必须进行实验。无论如何,当您收到鼠标事件时,您将使用新点构建一条路径,并一次渲染一点。如果你需要澄清,请告诉我

另外,对于
opacity
,您将在为您的上下文创建
CGColorRef
时设置它。。。
CoreGraphics/CGColor.h
中的许多调用都有alpha参数。

重复的

#pragma mark -
#pragma mark Touch Event

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

    lastPoint = [touch locationInView:drawView];
}

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

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

    UIGraphicsBeginImageContext(drawView.frame.size);

    [drawImage.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

}

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

    UIGraphicsBeginImageContext(drawView.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, drawView.frame.size.width, drawView.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    CGContextFlush(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}
诀窍是将笔刷笔划放在自己的缓冲区中,在将整个内容与背景混合之前,可以在缓冲区中正确地剪裁alpha

这里有一种方法:创建两个视图,一个用于背景,另一个用于线条。在俯视图中绘制alpha为1的直线!然后将整个前景视图的alpha值设置为0.5(或您想要使用的任何值)

这将防止半透明笔刷笔划自身增强。但是,两种不同的笔划相互交叉(如您的示例中所示)又如何呢。你想让十字路口变得更密集吗?如果是这样,则需要为每个笔刷笔划创建新视图。为了防止视图过多导致内存溢出,您需要将以前的俯视图与背景混合。

Hie

这是我最后的解决办法。。 现在,我正在drawRect事件上绘制线条,并将整条线条一次绘制为一条路径。。 这将创建不透明度以保留,现在不会在两者之间绘制线帽

毫无疑问,这是一个有效的技巧,因为我在每一次触摸中都会画画,而且效果很好

- (void)drawRect:(CGRect)rect {
for (int j=0; j<[pathArray count]; j++) 
{
    context = UIGraphicsGetCurrentContext();
    NSMutableDictionary *tmp=[pathArray objectAtIndex:j];
    NSMutableArray *currentPath=[tmp objectForKey:@"path"];
    for (int i=0; i<[currentPath count]; i++) 
    {
        CGPoint mid1 = [[self midPoint:[currentPath objectAtIndex:i+1]  :[currentPath objectAtIndex:i]] CGPointValue]; 
        CGPoint mid2 = [[self midPoint:[currentPath objectAtIndex:i+2] :[currentPath objectAtIndex:i+1]] CGPointValue];
        CGContextMoveToPoint(context, mid1.x, mid1.y);
        CGContextAddQuadCurveToPoint(context, [[currentPath objectAtIndex:i+1] CGPointValue].x, [[currentPath objectAtIndex:i+1] CGPointValue].y, mid2.x, mid2.y); 
        CGContextSetShadow(context, CGSizeMake(-2, -2), 3);
        CGContextSetAlpha(context, [[tmp objectForKey: @"opacity"] floatValue]);    
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetStrokeColorWithColor(context,[[tmp objectForKey: @"Color"] CGColor]);               
        CGContextSetLineWidth(context, [[tmp objectForKey: @"width"] floatValue]);              
        i+=2;
    }   
    CGContextStrokePath(context);

}
}
-(void)drawRect:(CGRect)rect{

对于(int j=0;jI我读了你的问题,但我不确定“线之间的重叠点”的含义。@psoft请看图片,画线时我可以看到圆圈。.它们是重叠点。.我不想要它们,我想要透明的线。.哦,好的…几分钟后看到我的答案。太好了。.希望它能解决我的问题。.CGContextSetLineCap(上下文,kCGLineCapButt);?我不是在画圆。它们是在我画路径时画的线帽。我尝试设置alpha参数,但结果如下:(你知道,我想得越多,我就越觉得你只需要为每个手势推送一个上下文,使路径不透明,然后在手势结束时,或者,我想,在每次触摸事件之后,使用你想要的alpha应用上下文。或者重新设计CGContext的斜接/连接逻辑,并绘制你自己的“元路径”,意思是你的路径周围的边界,然后填充元路径。但是一堆屏幕外的上下文应该可以。你认为呢?或者我认为,如果用户看到他的线条在手势过程中呈现为不透明,然后在停止后转换为半透明,这也不会太可怕。值得思考。但我认为这是你的解决方案。是吗这是一个解决方案,但不好:(可以说不可接受我没有收到你以前的评论..每次推上下文..如果我理解到了要点,那么你不认为这会使应用程序变得沉重吗?这对我没有帮助..因为alpha值是1.00..尝试alpha<1,你就会明白为什么这是一个挑战..这是我已经实现的东西..我想要一些编辑这是我完成任务所需要的。试着用alpha<1,你就会明白为什么这很有挑战性。但是在一些接触之后,这会变慢,而且每次都会画出整个矩形,这非常有挑战性slow@CrazyCreator你的代码中的“pathArray”是什么?我想这样做。你能帮我解决这个问题吗?@CrazyCreator你能给我看看1)触摸开始2)触摸移动和3)触摸结束方法?在触摸方法中,点被添加到路径数组@Coder。在印度白天,你可以在上找到我
- (void)drawRect:(CGRect)rect {
for (int j=0; j<[pathArray count]; j++) 
{
    context = UIGraphicsGetCurrentContext();
    NSMutableDictionary *tmp=[pathArray objectAtIndex:j];
    NSMutableArray *currentPath=[tmp objectForKey:@"path"];
    for (int i=0; i<[currentPath count]; i++) 
    {
        CGPoint mid1 = [[self midPoint:[currentPath objectAtIndex:i+1]  :[currentPath objectAtIndex:i]] CGPointValue]; 
        CGPoint mid2 = [[self midPoint:[currentPath objectAtIndex:i+2] :[currentPath objectAtIndex:i+1]] CGPointValue];
        CGContextMoveToPoint(context, mid1.x, mid1.y);
        CGContextAddQuadCurveToPoint(context, [[currentPath objectAtIndex:i+1] CGPointValue].x, [[currentPath objectAtIndex:i+1] CGPointValue].y, mid2.x, mid2.y); 
        CGContextSetShadow(context, CGSizeMake(-2, -2), 3);
        CGContextSetAlpha(context, [[tmp objectForKey: @"opacity"] floatValue]);    
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetStrokeColorWithColor(context,[[tmp objectForKey: @"Color"] CGColor]);               
        CGContextSetLineWidth(context, [[tmp objectForKey: @"width"] floatValue]);              
        i+=2;
    }   
    CGContextStrokePath(context);

}
}