2017-09-16 83 views
2

我创建一个覆盖的模糊视图与中间明确的矩形剪影。我的代码到目前为止已经取得了相反的结果,即在中间有一个模糊切口的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 
+0

我没有任何想法......但最后一行不应该存在blurView.layer.mask = scanLayer – Naresh

+0

使用此https://stackoverflow.com/questions/29647792/swift-how-to-create-a-view-with-a-shape-cropped-in-it请参阅DănuţMihai Florian的回答 – Naresh

回答

2

你可以像下面这样做

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 
+0

此解决方案工作!但我不明白这个概念。为什么需要有superlayerPath? – Koh

+0

因为你需要掩盖内部矩形周围的一切,而不是矩形本身。您可以添加此行以查看mask的工作方式let superlayerPath = UIBezierPath.init(ovalIn:blurView.frame) –

+0

非常感谢ans.Now我想在该区域添加阴影。如何可以。请帮助我。 @VitalyMigunov –