2017-12-08 13 views
1

내 목표는보기가 완전히 분명하다, 명확한 둥근 RECT 인세CAShapeLayer 컷 아웃 UIView의

let shape = CGRect(x: 0, y: 0, width: 200, height: 200) 

    let maskLayer = CAShapeLayer() 
    maskLayer.path = UIBezierPath(roundedRect: shape, cornerRadius: 16).cgPath 
    maskLayer.fillColor = UIColor.clear.cgColor 
    maskLayer.fillRule = kCAFillRuleEvenOdd 

    let background = UIView() 
    background.backgroundColor = UIColor.black.withAlphaComponent(0.8) 
    view.addSubview(background) 
    constrain(background) { 
     $0.edges == $0.superview!.edges 
    } 

    background.layer.addSublayer(maskLayer) 
//  background.layer.mask = maskLayer 

꽤 불투명 한 배경을 만드는 것입니다. 내가 주석을 달았을 때, 반투명 한 배경색을 보았지만 마스크 컷 아웃은 보이지 않았다.

내가 뭘 잘못하고 있나? 감사!

답변

1

필요하지 않은 것은 내가 의도 한 효과 (- 자신의 확장 것으로 보인다 마이너스 .edgesconstrain)를 구현 생각 놀이터입니다. (maskLayer.backgroundColor 세트 대신 maskLayer.fillColor

uiview 또 다른 층으로서 maskLayer 추가가 불필요

는 a) (단지)) 효과가 두드러

B를 만드는 대신 알파 블랙의 적색 배경을 사용 @Ken으로 표시)하지만 해를 끼치 지 않는 것으로 보입니다. 당신이 사진 프레임/색-봉투와 투명 창 효과 다음과 같은 의도 경우 반면에

image of clear inset

import UIKit 
    import PlaygroundSupport 


    let bounds = CGRect(x: 0, y: 0, width: 400, height: 200) 

    let uiview = UIView(frame: bounds) 
    uiview.backgroundColor = UIColor.red.withAlphaComponent(0.8) 

    PlaygroundPage.current.liveView = uiview 

    let shape = CGRect(x: 100, y: 50, width: 200, height: 100) 


    let maskLayer = CAShapeLayer() 
    maskLayer.path = UIBezierPath(roundedRect: shape, cornerRadius: 16).cgPath 
    maskLayer.backgroundColor = UIColor.clear.cgColor 
    maskLayer.fillRule = kCAFillRuleEvenOdd 
    uiview.layer.mask = maskLayer 
, 다음을 참조 gist

image of picture frame

0

마스크 레이어의 크기가 없습니다. 이 라인 maskLayer.frame = shape

추가 당신이 여기 background.layer.addSublayer(maskLayer)