CALayer with transparent hole in it

后端 未结 5 624
误落风尘
误落风尘 2020-11-28 17:28

I have a simple view (left side of the picture) and i need to create some kind of overlay (right side of the picture) to this view. This overlay should have some opacity, so

5条回答
  •  佛祖请我去吃肉
    2020-11-28 18:16

    I took a similar approach as animal_chin, but I'm more visual, so I set most of it up in Interface Builder using outlets and auto layout.

    Here is my solution in Swift

        //shadowView is a UIView of what I want to be "solid"
        var outerPath = UIBezierPath(rect: shadowView.frame)
    
        //croppingView is a subview of shadowView that is laid out in interface builder using auto layout
        //croppingView is hidden.
        var circlePath = UIBezierPath(ovalInRect: croppingView.frame)
        outerPath.usesEvenOddFillRule = true
        outerPath.appendPath(circlePath)
    
        var maskLayer = CAShapeLayer()
        maskLayer.path = outerPath.CGPath
        maskLayer.fillRule = kCAFillRuleEvenOdd
        maskLayer.fillColor = UIColor.whiteColor().CGColor
    
        shadowView.layer.mask = maskLayer
    

提交回复
热议问题