1

drawRect() メソッドの UIView を「元に戻したポリゴン」で塗りつぶす必要があります。ビュー内のすべてが、ポリゴン自体を除くいくつかの色で塗りつぶされます。

単純なポリゴンを描画するための次のコードがあります。

CGContextBeginPath(context);
for(int i = 0; i < corners.count; ++i)  
{
    CGPoint cur = [self cornerAt:i], next = [self cornerAt:(i + 1) % corners.count];
    if(i == 0)
        CGContextMoveToPoint(context, cur.x, cur.y);
    CGContextAddLineToPoint(context, next.x, next.y);
}
CGContextClosePath(context);
CGContextFillPath(context);

同様の質問を見つけましたが、Obj-C ではなく C# で: c# は GraphicsPath 以外のすべてを埋めます

4

3 に答える 3

3

おそらく最も速い方法は、クリップを設定することです:

// create your path as posted
// but don't fill it (remove the last line)

CGContextAddRect(context, self.bounds);
CGContextEOClip(context);

CGContextSetRGBFillColor(context, 1, 1, 0, 1);
CGContextFillRect(context, self.bounds);

他の両方の回答は、最初に四角形を塗りつぶしてから、上に透明な色で形状を描くことを示唆しています。どちらも必要なブレンド モードを省略しています。ここに作業バージョンがあります:

CGContextSetRGBFillColor(context, 1, 1, 0, 1);
CGContextFillRect(context, self.bounds);

CGContextSetBlendMode(context, kCGBlendModeClear);

// create and fill your path as posted

編集:どちらの方法backgroundColorでも、を NO に設定する必要がclearColorあります。opaque

2 番目の編集:元の質問は Core Graphics に関するものでした。もちろん、ビューの一部をマスキングする方法は他にもあります。最も顕著なのはCALayermaskプロパティです。

このプロパティを、クリップ パスを含む CAPathLayer のインスタンスに設定して、ステンシル効果を作成できます。

于 2012-08-29T13:27:59.360 に答える
0

drawRect では、ビューの背景色を希望の色に設定できます

    self.backgroundColor = [UIcolor redColor]; //set ur color

次に、あなたのやり方でポリゴンを描きます。

CGContextBeginPath(context);
for(int i = 0; i < corners.count; ++i)  
{
    CGPoint cur = [self cornerAt:i], next = [self cornerAt:(i + 1) % corners.count];
    if(i == 0)
        CGContextMoveToPoint(context, cur.x, cur.y);
    CGContextAddLineToPoint(context, next.x, next.y);
}
CGContextClosePath(context);
CGContextFillPath(context);

それが役立つことを願っています..幸せなコーディング:)

于 2012-08-29T12:55:05.380 に答える
0

新しい CGLayer を作成し、外側の色で塗りつぶしてから、クリア カラーを使用してポリゴンを描画します。

layer1 = CGLayerCreateWithContext(context, self.bounds.size, NULL);
context1 = CGLayerGetContext(layer1);

[... fill entire layer ...]

CGContextSetFillColorWithColor(self.context1, [[UIColor clearColor] CGColor]);

[... draw your polygon ...]

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawLayerAtPoint(context, CGPointZero, layer1);
于 2012-08-29T12:56:27.953 に答える