创建一个带有清晰剪切的blurView

时间:2017-09-16 11:49:32

标签: ios swift uivisualeffectview uiblureffect

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

我已经从以下帖子调整了我的步骤,如果有人能指出我正确的方向,我感激不尽。

Swift mask of circle layer over UIView

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

1 个答案:

答案 0 :(得分:3)

你可以这样做

    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