Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 在UIBezierPath图上应用渐变层_Swift_Core Graphics_Uibezierpath_Cagradientlayer - Fatal编程技术网

Swift 在UIBezierPath图上应用渐变层

Swift 在UIBezierPath图上应用渐变层,swift,core-graphics,uibezierpath,cagradientlayer,Swift,Core Graphics,Uibezierpath,Cagradientlayer,这是我第一次问问题,请原谅我问得不够彻底 基本上,我试图在绘制为图形的UIBezierPath顶部应用渐变层 这是我的图表: let path = quadCurvedPathWithPoints(points: points) let shapeLayer = CAShapeLayer() shapeLayer.path = path.cgPath shapeLayer.strokeColor = UIColor.blue.cgColor shapeLayer.fillColor = UICo

这是我第一次问问题,请原谅我问得不够彻底

基本上,我试图在绘制为图形的UIBezierPath顶部应用渐变层

这是我的图表:

let path = quadCurvedPathWithPoints(points: points)
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = UIColor.blue.cgColor
shapeLayer.fillColor = UIColor.white.cgColor
shapeLayer.position = CGPoint(x: 0, y: 0)
shapeLayer.lineWidth = 1.0

这是我想要的渐变层:

以下是我试图绘制的图表:

let path = quadCurvedPathWithPoints(points: points)
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = UIColor.blue.cgColor
shapeLayer.fillColor = UIColor.white.cgColor
shapeLayer.position = CGPoint(x: 0, y: 0)
shapeLayer.lineWidth = 1.0
很明显,这会用蓝色笔划填充图形,但现在我尝试将白色渐变应用到绿色渐变。我这样做是行不通的。结果如下:

以下是我正在努力解决的代码:

let startColor = UIColor.white.withAlphaComponent(0.5)
let endColor = UIColor.green

let gradient = CAGradientLayer()
gradient.colors = [startColor.cgColor, endColor.cgColor]
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
gradient.frame = path.bounds
let shapeMask = CAShapeLayer()
shapeMask.path = path.cgPath
gradient.mask = shapeLayer
sparkLineView.layer.addSublayer(gradient)

下面是一个使用渐变绘制
UIBezierPath
路径的小演示。你可以把它复制到Swift操场上

class GradientGraph: UIView {
    override func draw(_ rect: CGRect) {
        // Create the "graph"
        let path = UIBezierPath()
        path.move(to: CGPoint(x: 0, y: frame.height * 0.5))
        path.addLine(to: CGPoint(x: frame.width * 0.2, y: frame.height * 0.3))
        path.addLine(to: CGPoint(x: frame.width * 0.4, y: frame.height * 0.8))
        path.addLine(to: CGPoint(x: frame.width * 0.6, y: frame.height * 0.4))
        path.addLine(to: CGPoint(x: frame.width * 0.8, y: frame.height * 0.7))

        // Create the gradient
        let gradient = CGGradient(colorsSpace: nil, colors: [UIColor.white.cgColor,UIColor.green.cgColor] as CFArray, locations: nil)!

        // Draw the graph and apply the gradient
        let ctx = UIGraphicsGetCurrentContext()!
        ctx.setLineWidth(6)

        ctx.saveGState()
        ctx.addPath(path.cgPath)
        ctx.replacePathWithStrokedPath()
        ctx.clip()
        ctx.drawLinearGradient(gradient, start: CGPoint(x: 0, y: 0), end: CGPoint(x: frame.width, y: 0), options: [])
        ctx.restoreGState()
    }
}

let graph = GradientGraph(frame: CGRect(x: 0, y: 0, width: 700, height: 700))

哇!这正是我想做的!说真的,非常感谢。老兄,你是我的英雄!Thnx!