创建一个清晰的 blurView

Creating a blurView with a clear cut out

我正在创建一个 blurredView 的叠加层,中间有一个清晰的矩形切口。到目前为止,我的代码实现了相反的效果,即中间有一个模糊切口的 clearView。

我根据以下帖子调整了我的步骤,如果有人能指出正确的方向,我将不胜感激。

CALayer with transparent hole in it

let blurView = UIVisualEffectView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
blurView.effect = UIBlurEffect(style: UIBlurEffectStyle.dark)

let scanLayer = CAShapeLayer()
scanLayer.path = CGPath(rect: scanRect, transform: nil)

view.addSubview(blurView)
blurView.layer.addSublayer(scanLayer)
blurView.layer.mask = scanLayer

你可以这样做

    let scanLayer = CAShapeLayer()

    let scanRect = CGRect.init(x: 100, y: 100, width: 200, height: 100)

    let outerPath = UIBezierPath(rect: scanRect)

    let superlayerPath = UIBezierPath.init(rect: blurView.frame)
    outerPath.usesEvenOddFillRule = true
    outerPath.append(superlayerPath)
    scanLayer.path = outerPath.cgPath
    scanLayer.fillRule = kCAFillRuleEvenOdd
    scanLayer.fillColor = UIColor.black.cgColor

    view.addSubview(blurView)
    blurView.layer.mask = scanLayer