CAGradient layer not visible on view

我是研究僧i 提交于 2019-12-12 21:01:02

问题


I try to add a CAGradient effect in a pie chart I make with Core Graphics. It turns out I am not able and certainly not understand very well how to apply it to a view that is part of a CGPath...

override func draw(_ rect: CGRect)
    {
    ...
    func arc(myRadius: CGFloat, myStartAngle: CGFloat, myEndAngle: CGFloat) {
        // This is the pie chart segment
        context?.setFillColor(phenotypeColor.cgColor)
        context?.move(to: center)
        context?.addArc(center: center, radius: finalRadius, startAngle: myStartAngle, endAngle: myEndAngle, clockwise: true)
        context?.fillPath()

        (...)
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = self.bounds
        gradientLayer.colors = [UIColor.red, UIColor.blue]
        self.layer.addSublayer(gradientLayer)
    }

I don't see any gradient on the screen (only the white background...). Lastly, I also tried to clip the context juste after saving its state but I am not sure I clip anything since I don't see any gradient (either on the whole view or just on the segment). I read the documentation but it does not clear things up (in my case...).


回答1:


cgColor was missing... Sorry for the inconvenience.

let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.bounds
gradientLayer.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]
self.layer.addSublayer(gradientLayer)


来源:https://stackoverflow.com/questions/42807575/cagradient-layer-not-visible-on-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!