2016-08-30 4 views
0

Ich versuche, zwei rechteckige Pfade mit Kerngrafiken zu erstellen. Wenn ich versuche, den Pfad mit Farbe zu füllen, füllen die Überlappungen nicht die Farbe.CoreGraphics - Überlappender Pfadbereich, der Farbe nicht füllt

Der Code verwendet wird

- (void)drawRect:(CGRect)rect { 

    CGPoint topLeft = CGPointMake(121, 116); 
    CGPoint topRight = CGPointMake(221, 216); 

    CGPoint middleLeft = CGPointMake(121, 180); 
    CGPoint middleRight = CGPointMake(221, 280); 

    CGPoint bottomLeft = CGPointMake(250, 56); 
    CGPoint bottomRight = CGPointMake(350, 156); 

    CGMutablePathRef subpath1 = CGPathCreateMutable(); 
    CGPathMoveToPoint(subpath1, NULL, topLeft.x, topLeft.y); 
    CGPathAddLineToPoint(subpath1, NULL, topRight.x, topRight.y); 
    CGPathAddLineToPoint(subpath1, NULL, middleRight.x, middleRight.y); 
    CGPathAddLineToPoint(subpath1, NULL, middleLeft.x, middleLeft.y); 
    CGPathAddLineToPoint(subpath1, NULL, topLeft.x, topLeft.y); 
    CGPathCloseSubpath(subpath1); 

    CGMutablePathRef subpath2 = CGPathCreateMutable(); 
    CGPathMoveToPoint(subpath2, NULL, middleLeft.x, middleLeft.y); 
    CGPathAddLineToPoint(subpath2, NULL, middleRight.x, middleRight.y); 
    CGPathAddLineToPoint(subpath2, NULL, bottomRight.x, bottomRight.y); 
    CGPathAddLineToPoint(subpath2, NULL, bottomLeft.x, bottomLeft.y); 
    CGPathAddLineToPoint(subpath2, NULL, middleLeft.x, middleLeft.y); 
    CGPathCloseSubpath(subpath2); 

    CGMutablePathRef path = CGPathCreateMutable(); 
    CGPathAddPath(path, NULL, subpath1); 
    CGPathAddPath(path, NULL, subpath2); 
    CGPathCloseSubpath(path); 


    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.19 green:0.42 blue:0.09 alpha:1.0].CGColor); 
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.19 green:0.42 blue:0.09 alpha:1.0].CGColor); 
    CGContextSetBlendMode(context, kCGBlendModeMultiply); 
    CGContextSetAlpha(context, 1.0); 

    CGContextAddPath(context, path); 
    CGContextDrawPath(context, kCGPathFillStroke); 
} 

Der Ausgang ist

enter image description here

ich die Farbe Grün gefüllt werden wollen, dass alle geschlossenen Bereich. Kann mir jemand helfen, dieses Problem zu lösen?

Antwort

1

Sie füllen/streichen nur den gesamten Pfad, der aufgrund von Füllregeln dort Platz in Ihrem Pfad freigibt. Stattdessen sollten Sie den Hintergrundpfad zeichnen und streichen. Zeichnen Sie dann den Vordergrundpfad fill/stroke. Zum Beispiel:

CGContextAddPath(context, subpath1); 
CGContextDrawPath(context, kCGPathFillStroke); 

CGContextAddPath(context, subpath2); 
CGContextDrawPath(context, kCGPathFillStroke); 

könnten Sie auch mit UIBezierPath betrachten, wie es schöner ist meiner Meinung nach verwenden.