2013-03-12 141 views
11

我在touchesMoved中通過UIBezierPath繪製一個形狀。如何在UIBezierPath中填充顏色?

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    secondPoint = firstPoint; 
    firstPoint = [touch previousLocationInView:self]; 
    currentPoint = [touch locationInView:self]; 

    CGPoint mid1 = midPoint(firstPoint, secondPoint); 
    CGPoint mid2 = midPoint(currentPoint, firstPoint); 

    [bezierPath moveToPoint:mid1]; 
    [bezierPath addQuadCurveToPoint:mid2 controlPoint:firstPoint]; 

    [self setNeedsDisplay]; 
} 

我想在closePath後面填充RED顏色,但不能。請幫忙!

- (void)drawRect:(CGRect)rect 
{ 
    UIColor *fillColor = [UIColor redColor]; 
    [fillColor setFill]; 
    UIColor *strokeColor = [UIColor blueColor]; 
    [strokeColor setStroke]; 
    [bezierPath closePath]; 
    [bezierPath fill]; 
    [bezierPath stroke]; 
} 
+0

它看起來並不像你實際上撫摸着一條路徑,所以沒有一條路要關閉。 – Abizern 2013-03-12 10:33:54

+1

你能給個答案嗎?我不太瞭解。謝謝! – 2013-03-12 10:35:20

+0

讓我猜測?你沒有變形,只是一條線?每次觸摸移動時,都會關閉當前的子路徑。 – Abizern 2013-03-12 10:45:11

回答

16

如果您有存儲在別處貝塞爾路徑,這應該工作:

編輯

看你編輯的代碼,什麼情況是,當你正在關閉你正在繪製的路徑已經關閉 - 所以你得到一條線,而不是一個形狀,因爲你只有兩個點。

解決這個問題的方法之一是創建路徑,當你的點移動,但筆畫和填充該路徑的副本。例如這是未經測試的代碼,我寫它直接在

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    secondPoint = firstPoint; 
    firstPoint = [touch previousLocationInView:self]; 
    currentPoint = [touch locationInView:self]; 

    CGPoint mid1 = midPoint(firstPoint, secondPoint); 
    CGPoint mid2 = midPoint(currentPoint, firstPoint); 

    [bezierPath moveToPoint:mid1]; 
    [bezierPath addQuadCurveToPoint:mid2 controlPoint:firstPoint]; 

    // pathToDraw is an UIBezierPath * declared in your class 
    pathToDraw = [[UIBezierPath bezierPathWithCGPath:bezierPath.CGPath]; 

    [self setNeedsDisplay]; 
} 

然後繪圖代碼:

- (void)drawRect:(CGRect)rect { 
    UIColor *fillColor = [UIColor redColor]; 
    [fillColor setFill]; 
    UIColor *strokeColor = [UIColor blueColor]; 
    [strokeColor setStroke]; 

    // This closes the copy of your drawing path. 
    [pathToDraw closePath]; 

    // Stroke the path after filling it so that you can see the outline 
    [pathToDraw fill]; // this will fill a closed path 
    [pathToDraw stroke]; // this will stroke the outline of the path. 

} 

有一些整理工作做一個touchesEnded這可能是提高了性能,但你明白了。

+0

如何爲CAShapelayer上的封閉貝塞爾路徑填充顏色。對我來說,它始終是黑色的。 – Madhu 2015-01-23 13:18:38

-3

你必須保持你的陣列UIBezierPath s。試試這個代碼,

- (void)drawRect:(CGRect)rect 
    { 

     for(UIBezierPath *_path in pathArray){ 


     [_path fill]; 
     [_path stroke]; 
     } 
    } 

    #pragma mark - Touch Methods 
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     myPath=[[UIBezierPath alloc]init]; 
     myPath.lineWidth = currentSliderValue; 

     UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
     [myPath moveToPoint:[mytouch locationInView:self]]; 
     [pathArray addObject:myPath]; 
    } 
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     UITouch *mytouch=[[touches allObjects] objectAtIndex:0]; 
     [myPath addLineToPoint:[mytouch locationInView:self]]; 
     [self setNeedsDisplay]; 

    } 
+0

它不回答關於填充路徑的問題。這只是用於繪製具有觸摸的線條的罐裝代碼。 – Abizern 2013-03-12 10:39:08

+0

drawrect中的代碼是用於填充圖形的代碼。 – 2013-03-12 10:40:54