2017-09-16 11 views
2

중간에 명확한 사각형 컷 아웃이있는 blurredView의 오버레이를 만듭니다. 지금까지 내 코드는 반대로, 즉 중앙에 흐림 컷 아웃이있는 clearView를 구현했습니다.명확한 잘라 내기로 blurView 만들기

나는 다음과 같은 게시물에서 나의 발걸음을 적응시켰다. 누군가 올바른 방향으로 나를 가리킬 수 있다면 고맙겠다.

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

내부 사각형 주위의 모든 것을 마스크해야하지만 사각형 자체는 마스크하지 않아야합니다. 이 라인을 추가하여 마스크가 어떻게 작동하는지 확인할 수 있습니다. superlayerPath = UIBezierPath.init (ovalIn : blurView.frame) –

+0

대단히 고마워요. 이제 그 영역을 잘라내는 데 그림자를 추가하고 싶습니다. 어떻게 할 수 있습니까? 도움을주세요. 나를. @VitalyMigunov –