相交UIBezierPath上的填充路径

时间:2017-08-04 09:39:41

标签: ios iphone swift

有关如何填写此处所有路径的任何想法。目前发生的是我在我的视图上绘制一个矩形路径,然后在它们之间添加小圆圈,但似乎如果圆和矩形相交,则显示白色填充颜色。我想要的是显示渐变层。有帮助吗?我目前的代码如下。 enter image description here

    func addGradientLayer() {
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds

        let startColor = UIColor.create(withHexOrName: OurPayStatesViewUX.GradientColorStart)
        let endColor = UIColor.create(withHexOrName: OurPayStatesViewUX.GradientColorEnd)

        gradientLayer.colors = [startColor.CGColor, endColor.CGColor]
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint(x: 1, y: 0)
        layer.insertSublayer(gradientLayer, atIndex: 0)
    }

    func createOverlay(view: UIView, circleLocations: [CGPoint]) {

        maskLayer?.removeFromSuperlayer()

        let radius: CGFloat = view.frame.height/2
        let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: view.bounds.size.height), cornerRadius: 0)

        for i in circleLocations {
            // Create a circle path in each of the state views
            let circlePath = UIBezierPath(roundedRect: CGRect(x: i.x, y: i.y, width: 2 * radius, height: 2 * radius), cornerRadius: radius)
            path.appendPath(circlePath)
        }

        let rect = createRectangle(startPointX: 0, endPointX: view.bounds.size.width)
        path.appendPath(rect)

        path.usesEvenOddFillRule = true

        let fillLayer = CAShapeLayer()
        fillLayer.path = path.CGPath
        fillLayer.fillRule = kCAFillRuleEvenOdd
        fillLayer.fillColor = backgroundColor?.CGColor ?? UIColor.whiteColor().CGColor
        fillLayer.opacity = 1

        maskLayer = fillLayer
        layer.addSublayer(fillLayer)
    }

    func createRectangle(startPointX startPointX: CGFloat, endPointX: CGFloat) -> UIBezierPath {
        let rectHeight: CGFloat = 6
        let path = UIBezierPath(rect: CGRect(x: startPointX, y: frame.height/2 - rectHeight/2, width: endPointX - startPointX, height: rectHeight))

        return path
    }

0 个答案:

没有答案