10

カスタムコントロールサークルを作っています。円の一部が透明になる場合があります。透明ではなく半透明にした方が、より視覚的に意味があり、見栄えが良くなります。

ビューは長方形であり、残りの長方形ではなく円のみを半透明にしたいので、これは問題です。

は、カスタム コントロールのUIVisualEffectView背後にあります。

ここに画像の説明を入力

(デバッグ目的で円の内側にレンダリングされるものは何もありません)

ご覧のとおり、ビューは円の外側のものをぼやけさせています。

ビューだけ内部をぼかす方法がわからず、プレリリース ドキュメントはほとんど空です。私の唯一の考えは、円をカバーするために多くの 1x1 ビューを作成することですが、これは実際には機能しないように思われます。ビューの外側をぼかすことなく、ビュー内のコンテンツをぼかすにはどうすればよいですか?

4

1 に答える 1

30

円ビューのレイヤー マスクを塗りつぶした円に設定します。

CircleControlView *circleView = yourCircleView();

CAShapeLayer *mask = [CAShapeLayer layer];
mask.path = [UIBezierPath bezierPathWithOvalInRect:circleView.bounds].CGPath;
circleView.layer.mask = mask;

アップデート

Swift プレイグラウンドの例:

import UIKit
import XCPlayground
import QuartzCore

UIGraphicsBeginImageContext(CGSize(width: 100, height: 100))
let viewPath = UIBezierPath(ovalInRect:CGRect(x:1,y:1,width:98,height:98))
viewPath.lineWidth = 2
viewPath.stroke()
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

let view = UIView(frame:CGRect(x:0,y:0,width:100,height:100))
XCPShowView("view", view)

let label = UILabel(frame:view.bounds)
label.text = "This is the text in the background behind the circle."
label.numberOfLines = 0
view.addSubview(label)

let effectView = UIVisualEffectView(effect:UIBlurEffect(style:.ExtraLight))
effectView.frame = view.bounds
view.addSubview(effectView)

let circleView = UIImageView(image:image)
effectView.addSubview(circleView)

let maskPath = UIBezierPath(ovalInRect:circleView.bounds)
let mask = CAShapeLayer()
mask.path = maskPath.CGPath
effectView.layer.mask = mask

結果:

半透明の円

于 2014-08-19T20:53:36.363 に答える