2012-02-23 37 views
2

毫無疑問,這可能是一個重複的問題,但我無法從這裏的任何帖子獲得適當的解決方案。所以我張貼這個作爲新的職位,希望我得到一些解決方案。如何在alpha 2中繪製α<1的石英線

我的繪圖應用程序我想給線條繪製不透明的功能。 但是,當我將alpha賦值給顏色時,我得到了該行之間的重疊點。

有些地方我發現在兩個視圖中繪圖解決了這個問題,但我無法理解背後的邏輯。

enter image description here

我使用普通石英二維碼吸取觸摸事件線。

+0

我讀過你的問題,但我不確定'行之間的重疊點'的含義。 – QED 2012-02-23 06:19:06

+0

@psoft在圖片中看到,雖然畫線我可以看到圈子..他們是重疊的點..我不想他們,我想要透明線.. – DivineDesert 2012-02-23 06:21:04

+0

哦好吧......在幾分鐘內看到我的答案。 – QED 2012-02-23 06:23:14

回答

1

枝..

這是我最後的解決方案.. 我現在借鑑的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); 

} 
} 
+1

但是這會減慢一些觸摸 – 2012-06-21 12:19:02

+0

也繪製整個矩形,每次都很慢 – jjxtra 2012-09-30 21:13:22

+0

@CrazyCreator什麼是你的代碼中的「pathArray」?我想這樣做。你能幫我解決這個問題嗎? – Hitarth 2012-12-20 07:33:28

0

看起來您正在一系列觸摸事件的每個點上繪製一個圓。相反,您可以使用CoreGraphics/Quartz2D生成Path,將線寬設置爲任意厚度,然後根據需要對該路徑進行筆劃,以保持界面的美觀。我有一段時間沒有這樣做,但我認爲你需要的大部分將在CoreGraphics/CGContext.h~/CGPath.h, etc.看到我對另一個CoreGraphics問題的答案here

現在我腦海中一個未知的是你是否可以在使用CGPathCloseSubpath()「關閉」它之前對CGMutablePathRef進行描述。你必須嘗試。無論如何,當你收到鼠標事件時,你將建立一個新的點,並一次渲染一點點。讓我知道你是否需要澄清。

P.S.至於opacity,您將設置爲它爲您的上下文創建CGColorRef時...... CoreGraphics/CGColor.h中的許多調用都具有alpha參數。

+0

我不是繪製圓圈..他們是當我繪製路徑時繪製的線條帽..我嘗試設置alpha參數,但它給出了這個結果:( – DivineDesert 2012-02-23 06:34:40

+0

oooh我看到。接受挑戰。 – QED 2012-02-23 06:46:29

+0

你試過CGContextSetLineJoin()嗎? – QED 2012-02-23 06:51:13

-1
#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(); 
} 
+0

這不會幫助我..因爲阿爾法值是1.00 ..嘗試與阿爾法<1,你會得到爲什麼這是具有挑戰性。 – DivineDesert 2012-02-23 09:05:04

+0

它可以是有幫助的,如果你可以做一些編輯這個代碼比... ...所有最好的! – Developer 2012-02-23 09:20:17

+0

這是我已經實施的東西..我想要一些編輯,這是需要得到我的東西.. – DivineDesert 2012-02-23 09:25:58

3

In CoreGraphics drawing how can I keep the point of overlap from being darker than the rest of the line?

訣竅複製是有筆觸在自己的緩衝區,在那裏你可以與背景融合了整個事情之前適當剪裁阿爾法。

以下是一種方法:創建2個視圖,一個用於背景,另一個用於行。在alpha視圖中繪製頂部視圖中的線條!然後將整個前景視圖的alpha設置爲0.5(或者任何你想使用的值)。

[topView setAlpha:0.5]; 

這樣可以防止半透明畫筆筆劃強化自身。但是,2個不同的筆觸互相交叉(如你的例子)。你想讓那個路口更加激烈嗎?如果是這樣,那麼您需要爲每個筆觸創建一個新視圖。爲了避免因視圖太多而導致內存溢出,您需要將先前的頂部視圖與背景混合。

+0

非常感謝。 – 2012-09-27 12:13:29