2013-03-12 14 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

+1

あなたは答えをくれませんか?それほど分かりません。ありがとう! –

+0

私は推測させてください?あなたは形をしていない、ただの線ですか?タッチが動くたびに、現在のサブパスが閉じます。 – Abizern

答えて

16

、このコードを試してみてください。

編集

何が起こっているのか、編集したコードを見てみると、あなたが閉じているとということですあなたが描画しているパスが閉じている - あなたは2つのポイントしか持っていないので、あなたは形状ではなくラインを取得します。

これを回避する方法の1つは、ポイントの移動に合わせてパスを作成することですが、そのパスのコピーをストロークして入力する必要があります。たとえばの場合、これは未テストコードですが、私は

-(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

-3

UIBezierPathの配列を維持する必要があります。あなたが他の場所に格納されたベジエパスを持っている場合、これは動作するはず

- (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

+0

drawrectのコードは、図面を塗りつぶすためのコードです。 –

関連する問題