2017-09-16 4 views
2

でblurViewを作成します。これまでのところ、私のコードでは逆の結果が得られました。つまり、真ん中にぼかしを入れたclearViewです。私は途中で明確な矩形の切り欠きとblurredViewのオーバーレイを作成しています明確なカットアウト

私は次の投稿からステップを順応しました。誰かが正しい方向に向けることができたらうれしいです。

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-itDă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

あなたは、内側の長方形ではなく、長方形自体の周りのすべてをマスクする必要があるため。この行を追加すると、マスクがどのように動作しているのかを確認できます。superlayerPath = UIBezierPath.init(ovalIn:blurView.frame) –

+0

大変ありがとうございます。今、その領域に影を追加したいと思います。私。 @VitalyMigunov –

関連する問題