如何在圆圈内使用UIVisualEffectView

我正在制作一个自定义控制圈。 圆的一部分可以是透明的。 如果它是半透明的而不是透明的,它会更具视觉感并且看起来更好。

因为视图是矩形的,我只希望圆是半透明的,而不是矩形的其余部分,这是一个问题。

UIVisualEffectView位于自定义控件之后。

在此处输入图像描述

(没有任何在圆圈内呈现的内容用于调试目的)

如您所见,视图模糊了圆圈之外的东西。

我不知道如何仅在视图内部模糊,并且预发布文档几乎是空的 。 我唯一的想法是创建许多1×1视图来覆盖圆圈,但这似乎不会真正起作用,即使它确实如此,这将是一个缓慢而丑陋的解决方案。 如何模糊视图中的内容,而不模糊外部的任何内容?

将圆形视图的图层蒙版设置为实心圆:

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

UPDATE

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 

结果:

半透明的圆圈