6

プラス記号の形をしたクリッピングパスを作成しようとしています。これにより、同じコンテキストに描画する後続のパスでこの部分が削除されます。2つの長方形のパスを重ねて使用してクリッピングパスを作成します。

これは、後で円を描くときに最終的な描画がどのようになるかを示しています。

      xXX | | XXx
   XXXX | | XXXX
 XXXXX | | XXXXX
 ———&nbsp; ——— <br /> ———&nbsp; ——— <br /> XXXXX | | XXXXX
   XXXX | | XXXX
      xXX | | XXx

ただし、実際には次のようになります。

      xXX | | XXx
   XXXX | | XXXX
 XXXXX | | XXXXX
 ———&nbsp; XX ——— <br /> ———&nbsp; XX ——— <br /> XXXXX | | XXXXX
   XXXX | | XXXX
      xXX | | XXx

この動作を正しく読んだ場合、2つの長方形のパスの交点はクリッピングマスクの一部を形成していません。

この場合、appendPathは2つの長方形のパスから単一の統合パスを作成しないようです-これについては何もできないと思います。さらに、CoreGraphicsにはパスユニオンなどに関連する機能がないようです。

誰かが私に何ができるか考えていますか?関連するコードスニペットを含めました。

クリッピングマスクに他の重複するパスを追加したいので、1つのパスを使用してプラス記号を描画することは解決策ではありません。

        CGContextSaveGState(context);

        // create clipping path
        UIBezierPath *clippingPath = [UIBezierPath bezierPath];
        clippingPath = [UIBezierPath bezierPathWithRect:CGRectMake(centrePoint.x - 2.0f, 0.0f, 4.0f, self.sizeY)];
        [clippingPath appendPath:[UIBezierPath bezierPathWithRect:CGRectMake(0.0f, centrePoint.y - 2.0f, self.sizeX, 4.0f)]];

        // use the clipping path to create a hole in the context
        CGContextAddPath(context, clippingPath.CGPath);
        CGRect boundingRect = CGContextGetClipBoundingBox(context);
        CGContextAddRect(context, boundingRect);
        CGContextEOClip(context);

        // draw the icon shape (clipped portion is removed)
        iconBezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(self.sizeX / 3.0f, self.sizeY / 2.25f, self.sizeX / 3.0f, self.sizeX / 3.0f)];

        [highlightColor setFill];
        [iconBezierPath fill];
        CGContextRestoreGState(context);
4

1 に答える 1

3

を使用して、交差点でノックアウトされたピースを元に戻すことができますCGRectIntersection

    CGContextSaveGState(context);

    CGRect rect1 = CGRectMake(centrePoint.x - 2.0f, 0.0f, 4.0f, self.sizeY);
    CGRect rect2 = CGRectMake(0.0f, centrePoint.y - 2.0f, self.sizeX, 4.0f);
    CGRect rect3 = CGRectIntersection(rect1, rect2);

    CGContextAddRect(context, rect1);
    CGContextAddRect(context, rect2);
    CGContextAddRect(context, rect3);

    CGRect boundingRect = CGContextGetClipBoundingBox(context);
    CGContextAddRect(context, boundingRect);
    CGContextEOClip(context);

        // draw the icon shape (clipped portion is removed)
    iconBezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(self.sizeX / 3.0f, self.sizeY / 2.25f, self.sizeX / 3.0f, self.sizeX / 3.0f)];

    [highlightColor setFill];
    [iconBezierPath fill];

    CGContextRestoreGState(context);

これは質問の要件を満たしていますが、ニーズを完全に満たすかどうかは、「他の重複するパス」の性質に依存します。

ここに画像の説明を入力

于 2013-03-23T05:15:26.467 に答える