简体   繁体   English

带有遮罩层的UIVisualEffectView

[英]UIVisualEffectView with mask layer

I am trying to blur a MKMapView while also displaying a circle mask above it. 我试图模糊MKMapView,同时在它上面显示一个圆形面具。 To better visualize what I mean with this you can find an image of my current state attached: 为了更好地形象化我的意思,您可以找到我当前所附状态的图像:

当前状态

This shows nearly what I want but the background (the map) should be blurred which is not the case in this picture. 这显示了我想要的几乎,但背景(地图)应该模糊,这不是这张图片中的情况。 I tried to work with UIVisualEffectView, but it seems I am doing something wrong there. 我尝试使用UIVisualEffectView,但似乎我在那里做错了。 Here is what I tried: 这是我尝试过的:

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
}

To summarize here is my question: How do I have to change my code in order to blur the mapview aswell as show the cirlce mask above it ? 总结这里是我的问题: 我如何更改我的代码以模糊mapview以及显示它上面的cirlce面具?

EDIT Just found out that the blur of the mapview works if I remove the code to add the circle mask... Maybe this is of any interest in order to solve this problem. 编辑刚刚发现如果我删除代码以添加圆形蒙版,mapview的模糊效果......也许这对于解决这个问题是有意义的。

Ive now found a solution after digging around for a bit. 我现在找到一个解决方案后挖了一下。 Im assuming you're using Xcode 8 as the layer.mask has a known bug where you cannot have both a blur, and a mask on the same layer. 我假设你使用Xcode 8作为layer.mask有一个已知的错误,你不能同时拥有模糊和掩码。

After messing around with a playground I have fixed the problem, so will try to adapt your code to match my solution. 在操场上玩弄后我已经修复了问题,因此会尝试调整您的代码以匹配我的解决方案。 If you use the "mask" property of the blurView instead, then you should have no issues. 如果您使用blurView的“mask”属性,那么您应该没有问题。 There has been a bug report made to Apple I believe in regards to the layer.mask not working 有一个错误报告发给Apple我相信关于layer.mask不起作用

This is your current code at the end 这是您最后的当前代码

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

Instead of this, try using the following: 而不是这样,尝试使用以下内容:

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

Let me know if you have any issues! 如果您有任何问题,请告诉我!

In addition to Jack's solution, if you are on iOS >11 then just set blur.layer.mask to the maskLayer: 除了Jack的解决方案,如果你在iOS> 11,那么只需将blur.layer.mask设置为maskLayer:

if #available(iOS 11.0, *) {
    blur.layer.mask = maskLayer
} else {
    let maskView = UIView(frame: self.view.frame)
    maskView.backgroundColor = UIColor.black
    maskView.layer.mask = borderLayer
    blur.mask = maskView
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM