2016-08-30 10 views
0

私はコアグラフィックを使用して2つの長方形のパスを作成しようとしています。色を使ってパスを塗り潰そうとすると、オーバーラップした色が塗りつぶされません。CoreGraphics - 色が塗りつぶされていない重複したパスエリア

が出力

enter image description here

私は緑色が全て閉じた領域を充填する必要

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

で使用されるコードです。この問題を解決する方法を教えてください。

答えて

1

パス全体を塗りつぶす/塗りつぶすのは、塗りつぶしルールによってパス内に空白ができてしまうためです。代わりに、背景パスの塗りつぶし/ストロークを描画する必要があります。その後、フォアグラウンドパスの塗りつぶし/ストロークを描画します。例えば:私の意見で使用するのに良くあるよう

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

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

またUIBezierPathを使用して検討するかもしれません。

関連する問題