1

3.2以降のiPadアプリを作成しています。私のアプリには、その下のすべてを暗くする半透明のオーバーレイビューがあります。このビューの真ん中で、このコードを使用して、この半透明の穴を切り刻んで、背景の一部を無傷で通過させています。

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect intersection = CGRectIntersection(hole.frame, rect);
    CGContextClearRect(context, intersection);
}

さらに、「穴」ビューには丸みを帯びた角があり、次の方法で適用されます。

self.layer.cornerRadius = 4.25;

これは、1つの小さな問題を除いてうまく機能します。これらの丸みを帯びた角は考慮されていないため、切り取られた穴は丸みを帯びているのではなく、四角い角になっています。これを修正する必要がありますが、方法がわかりません。アイデア、例、考えはありますか?

4

3 に答える 3

3

これが私がそれを機能させることになった方法です。これにより、「穴」UIViewと同じフレームの穴が生成され、自己から切り取られます(UIView)。これにより、「穴」の背後にあるものを妨げられずに確認できます。

- (void)drawRect:(CGRect)rect {
    CGFloat radius = self.hole.layer.cornerRadius;
    CGRect c = self.hole.frame;
    CGContextRef context = UIGraphicsGetCurrentContext();

        // this simply draws a path the same shape as the 'hole' view
    CGContextMoveToPoint(context, c.origin.x, c.origin.y + radius);
    CGContextAddLineToPoint(context, c.origin.x, c.origin.y + c.size.height - radius);
    CGContextAddArc(context, c.origin.x + radius, c.origin.y + c.size.height - radius, radius, M_PI_4, M_PI_2, 1);
    CGContextAddLineToPoint(context, c.origin.x + c.size.width - radius, c.origin.y + c.size.height);
    CGContextAddArc(context, c.origin.x + c.size.width - radius, c.origin.y + c.size.height - radius, radius, M_PI_2, 0.0f, 1);
    CGContextAddLineToPoint(context, c.origin.x + c.size.width, c.origin.y + radius);
    CGContextAddArc(context, c.origin.x + c.size.width - radius, c.origin.y + radius, radius, 0.0f, -M_PI_2, 1);
    CGContextAddLineToPoint(context, c.origin.x + radius, c.origin.y);
    CGContextAddArc(context, c.origin.x + radius, c.origin.y + radius, radius, -M_PI_2, M_PI, 1);

        // finish
    CGContextClosePath(context);
    CGContextClip(context); // this is the secret sauce
    CGContextClearRect(context, c);
}
于 2010-12-09T20:57:45.860 に答える
1

あなたがやろうとしていることは、マスキングと呼ばれます。Core Graphics を使用して、現在のグラフィックス コンテキストをマスクできます。この件に関する Apple のドキュメントを参照してください。

http://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_images/dq_images.html#//apple_ref/doc/uid/TP30001066-CH212-CJBHDDBE

于 2010-12-03T23:00:31.133 に答える
0

cornerRadiusレイヤーのプロパティを変更する場合は、コンテンツを丸みを帯びた角にクリップするために、関連するビューでもに設定clipsToBoundsする必要があります。YES

于 2010-12-03T21:48:48.293 に答える