2013-03-12 12 views
11

UIBezierPath tarafından bir şekil çiziyorum.UIBezierPath içinde renk nasıl doldurulur?

-(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'ten sonra KIRMIZI rengi doldurmak istiyorum, ancak yapamıyorum. Lütfen yardım et!

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

Aslında bir yolu okşadığınıza benzemiyor, dolayısıyla kapanacak bir tane yok. – Abizern

+1

Bir cevap verebilir misiniz? Ben çok anlamıyorum. Teşekkürler! –

+0

Tahmin edeyim mi? Bir şekil almıyorsun, sadece bir çizgi? Dokunuşunuz her hareket ettiğinde geçerli alt yolu kapatır. – Abizern

cevap

16

, bu çalışması gerekir:

Düzenleme

Düzenlenen kod baktığımızda ne oluyor size kapanış olarak olmasıdır çizdiğiniz yol kapanıyor - böylece sadece iki noktaya sahip olduğunuzdan, bir şekil değil, bir biçim elde edersiniz.

Bunun bir yolu, puanlarınız hareket ettikçe yol oluşturmak, ancak kontur ve bu yolun bir kopyasını doldurmaktır. Örneğin için bu denenmemiş kod, doğruca

-(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]; 
} 

bunu yazıyorum edilir Sonra çizim kodu yapabilirsiniz:

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

} 

bazı bir touchesEnded yapmak toplama vardır ve bu olabilir Daha fazla performans gösterdi, ama fikri anladın.

+0

CAShapelayer üzerindeki kapalı bir bezirepath için renk nasıl doldurulur. Bana göre daima siyah renklidir. – Madhu

-3

UIBezierPath s dizininizi saklamak zorundasınız. Başka bir yerde saklanan bir Bezier yolu varsa bu kodu deneyin,

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

Yolu doldurma ile ilgili soruya cevap vermiyor. Bu dokunuşlarla bir çizgi çizmek için sadece kod kodudur. – Abizern

+0

kodu, doğru çizmeyi doldurmak için kullanılan koddur. –