Swift UIView 镂空

Swift UIView 镂空

func showGuidage() {


        // 镂空

        //透明区域

        let nRect = CGRect.init(x: zScaleDev(iPhoneX: 15, iPadX: 54), y: zScaleDev(iPhoneX: 0, iPadX: 0), width: zScaleDev(iPhoneX: 345, iPadX: 727), height: zScaleDev(iPhoneX: 160, iPadX: 185))


        // 不能直接加在最底层viewController.view上面,会黑掉 后面没东西显示了

        let backgroundView = UIView()

        backgroundView.frame = CGRect.init(x: (zScreenW - zScaleDev(iPhoneX: 375, iPadX: 834)) / 2 , y: zNavBarH, width: zScaleDev(iPhoneX: 375, iPadX: 834), height:  zScaleDev(iPhoneX: 970, iPadX: 1112))

        view.addSubview(backgroundView)


        let maskLayer = CAShapeLayer()

        maskLayer.fillRule = CAShapeLayerFillRule.evenOdd //  奇偶层显示规则

        maskLayer.fillColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.6).cgColor

        maskLayer.opacity = 0.8

        backgroundView.layer.addSublayer(maskLayer)


        let basicPath = UIBezierPath.init(roundedRect: backgroundView.bounds, cornerRadius: 0) // 底层

        let maskPath = UIBezierPath.init(roundedRect: nRect, cornerRadius: zScale(x: 14))

        basicPath.append(maskPath) // 重叠

        basicPath.usesEvenOddFillRule = true

        maskLayer.path = basicPath.cgPath

    }

你可能感兴趣的:(Swift UIView 镂空)