带遮罩层的UIVisualEffectView

我想模糊一个MKMapView,同时还显示一个圆形的面具上面。 为了更好地展示我的意思,你可以find我现在状态的图像:

当前状态

这几乎显示了我想要的东西,但背景(地图)应该模糊,而不是在这张图片中的情况。 我试图与UIVisualEffectView工作,但似乎我在那里做错了。 这是我试过的:

func createOverlay(at: CGPoint) { var blur: UIView! blur = UIVisualEffectView (effect: UIBlurEffect (style: UIBlurEffectStyle.dark)) blur.frame = self.mapView.frame blur.isUserInteractionEnabled = false self.mapView.addSubview(blur) let circleSize: CGFloat = 200 let path = UIBezierPath ( roundedRect: blur.frame, cornerRadius: 0) let circle = UIBezierPath ( roundedRect: CGRect (origin: CGPoint(x:at.x - circleSize/2, y: at.y-circleSize/2), size: CGSize (width: circleSize, height: circleSize)), cornerRadius: circleSize/2) path.append(circle) path.usesEvenOddFillRule = true let maskLayer = CAShapeLayer() maskLayer.path = path.cgPath maskLayer.fillRule = kCAFillRuleEvenOdd let borderLayer = CAShapeLayer() borderLayer.path = circle.cgPath borderLayer.strokeColor = UIColor.white.cgColor borderLayer.lineWidth = 10 blur.layer.addSublayer(borderLayer) blur.layer.mask = maskLayer } 

总结这里是我的问题: 我怎样才能改变我的代码,以模糊mapview以及显示它上面的cirlce面具?

编辑只是发现,模糊的mapview工程,如果我删除代码添加圆形面具…也许这是任何兴趣,以解决这个问题。

我现在找了一个解决scheme后挖了一下。 我假设你使用Xcode 8作为layer.mask有一个已知的错误,你不能同时在一个模糊和一个掩码。

在操场上乱搞之后,我已经解决了这个问题,所以会尝试调整你的代码来匹配我的解决scheme。 如果您使用blurView的“mask”属性,那么您应该没有问题。 我已经向苹果公司提出了一个错误报告,我认为这个报告是关于layer.mask不起作用的

这是你最后的代码

 let maskLayer = CAShapeLayer() maskLayer.path = path.cgPath maskLayer.fillRule = kCAFillRuleEvenOdd let borderLayer = CAShapeLayer() borderLayer.path = circle.cgPath borderLayer.strokeColor = UIColor.white.cgColor borderLayer.lineWidth = 10 blur.layer.addSublayer(borderLayer) blur.layer.mask = maskLayer 

而不是这个,请尝试使用以下内容:

 let maskLayer = CAShapeLayer() maskLayer.path = path.cgPath maskLayer.fillRule = kCAFillRuleEvenOdd let borderLayer = CAShapeLayer() borderLayer.path = circle.cgPath borderLayer.strokeColor = UIColor.white.cgColor borderLayer.fillColor = UIColor.clear.cgColor //Remember this line, it caused me some issues borderLayer.lineWidth = 10 let maskView = UIView(frame: self.view.frame) maskView.backgroundColor = UIColor.black maskView.layer.mask = maskLayer blur.layer.addSublayer(borderLayer) blur.mask = maskView 

让我知道如果你有任何问题!