2013-03-12 14 views
11

Rysuję kształt według UIBezierPath w dotknięciach Move.Jak wypełnić kolor wewnątrz 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]; 
} 

Chcę wypełnić czerwony kolor wewnątrz niego po ścieżce closePath, ale nie może. Proszę pomóż!

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

Wygląda na to, że właściwie nie obrysowuje się ścieżki, więc nie można jej zamknąć. – Abizern

+1

Czy możesz udzielić odpowiedzi.Nie rozumiem tak wiele. Dzięki! –

+0

Niech zgadnę? nie dostaniesz kształtu, tylko linię? Za każdym razem, gdy poruszasz dotykiem, zamyka bieżącą ścieżkę podrzędną. – Abizern

Odpowiedz

16

Jeśli masz ścieżkę bezier przechowywane w innym miejscu, to powinno działać:

Edit

Patrząc na edytowanego kodu, co się dzieje jest, że jak się zamykają Ścieżka, którą rysujesz, zamyka się, więc otrzymujesz linię, a nie kształt, ponieważ masz tylko dwa punkty.

Jednym ze sposobów obejścia tego jest utworzenie ścieżki w miarę przesuwania się punktów, ale obrys i wypełnienie kopii tej ścieżki. Na przykład to jest kod niesprawdzone, piszę go prosto w

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

A potem kod rysunek może:

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

} 

Istnieją pewne uporządkowanie aby zrobić touchesEnded i to może być bardziej wydajne, ale masz pomysł.

+0

Jak wypełnić kolor zamkniętej ślepej ścieżki na CAShapelayer. Dla mnie jest zawsze czarny. – Madhu

-3

Musisz zachować tablicę swoich UIBezierPath s. Spróbuj tego kodu,

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

Nie odpowiada na pytanie o wypełnienie ścieżki. To jest po prostu kod puszkowy do rysowania linii z dotknięciem. – Abizern

+0

kod w drawrect to kod używany do wypełnienia rysunku. –