Mask a UIView with a cut-out circle

前端 未结 3 1025
南旧
南旧 2020-12-13 21:09

I’ve created a UIView with a semi-transparent black background to sit across my main app’s view. What I’m aiming for here is to create a cut-out circle shape so

3条回答
  •  庸人自扰
    2020-12-13 21:26

    Here is my conversion of the accepted answer in Swift:

    public class OverlayView: UIView {
    let fillColor: UIColor = UIColor.grayColor()
    public var framesToCutOut: Array = [NSValue]()
    
    override public func drawRect(rect: CGRect) {
        super.drawRect(rect)
    
        fillColor.setFill()
        UIRectFill(rect)
    
        if let context: CGContextRef = UIGraphicsGetCurrentContext() {
            CGContextSetBlendMode(context, .DestinationOut)
    
            for value in framesToCutOut {
                let pathRect: CGRect = value.CGRectValue()
                let path: UIBezierPath = UIBezierPath(ovalInRect: pathRect)
                path.fill()
            }
    
            CGContextSetBlendMode(context, .Normal)
        }
    }
    

提交回复
热议问题