2013-03-12 16 views
11

Disegno una forma di UIBezierPath nei tocchi spostati.Come riempire il colore all'interno di 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]; 
} 

Desidero riempire il colore ROSSO al suo interno dopo closePath ma non è possibile. Per favore aiuto!

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

Non sembra che si stia effettivamente accarezzando un percorso, quindi non ce n'è uno da chiudere. – Abizern

+1

Potresti dare una risposta. Non capisco molto. Grazie! –

+0

Fammi indovinare? non hai una forma, solo una linea? Ogni volta che il tuo tocco si sposta, chiude il sottotracciato corrente. – Abizern

risposta

16

Se si dispone di un tracciato Bezier memorizzato altrove, questo dovrebbe funzionare:

Modifica

Guardando il tuo codice modificato, ciò che sta accadendo è che, come si sta chiudendo il percorso che stai disegnando si sta chiudendo, quindi ottieni una linea, non una forma poiché hai solo due punti.

Un modo per aggirare questo è creare il percorso mentre i punti si spostano, ma traccia e riempie una copia di quel percorso. Per esempio questo è codice non testato, sto scrivendo in linea retta

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

E poi il tuo codice di disegno può:

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

} 

C'è un po 'di mettere in ordine per fare un touchesEnded e questo potrebbe essere reso più performante, ma ti viene l'idea.

+0

Come riempire il colore per un bezirepath chiuso su CAShapelayer. Per me è sempre di colore nero. – Madhu

-3

Devi mantenere la matrice del tuo UIBezierPath s. Prova questo codice,

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

Non risponde alla domanda sulla compilazione del percorso. Questo è solo un codice in scatola per disegnare una linea con tocchi. – Abizern

+0

il codice in drawrect è il codice che viene utilizzato per riempire il disegno. –