2012-07-16 26 views
1

我試圖做出對畫筆的不透明度控制的繪圖應用程序,但是當我試圖降低不透明度的結果是這樣的。我使用核心圖形。 (檢查圖像)。的Objective-C:觸摸抽獎不透明

enter image description here

我將如何解決這個問題?

下面是我的一些代碼。

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

看來你的問題是使用混合模式的結果,以及你重複向圖像添加單獨路徑的事實。你可以選擇下面的NSResponder和我自己描述的技術,或者你可以關閉混合模式,把你正在繪製的路徑放在你顯示在主圖像前的透明圖像上,然後你贏了用混合模式繪製彼此重疊的單個路徑的結合效果。 – Rob 2012-07-16 15:05:23

回答

1

不要在-touchesMoved:withEvent:中進行繪圖。在任何情況下,您應該更新需要繪製的內容,然後發送 - setNeedsDisplay.

由於您的代碼是在上面編寫的,因此您正在從事件消息獲得的每對位置之間創建路徑。

你應該-touchesBegan:withEvent:創建路徑,並添加到它的-touchesMoved:withEvent:

+0

這是行之有效的,但我有按鈕,將改變畫筆的顏色,我不能使用這種方式進行整合。 – SeongHo 2012-08-11 03:09:59

1

而不是每更新圖像touchesMoved,我建議你不要每次重新保存圖像,而是將每個touchesMoved添加到數據點的數組。然後,在繪圖程序(如NSResponder類建議)拉起原始圖像,然後通過重繪貴點陣列映射出整個路徑。如果您想在某個時候更新圖片,請在touchesEnded上執行,但不是每個touchesMoved

+1

不,他需要更新他在-touchesMoved中所畫的內容,他不應該爲每個事件創建一條路徑,就像他現在一樣。 – NSResponder 2012-07-16 11:23:42

+0

@NSResponder我認爲這就是我所說的,不是爲每個事件創建一個新的路徑,而是收集繪圖方法將用來創建路徑的數據點。我不理解你的評論。 – Rob 2012-07-16 14:29:32