CAGradient图层在视图上不可见 [英] CAGradient layer not visible on view
问题描述
我尝试在用Core Graphics制作的饼图中添加CAGradient效果。事实证明,我无法并且肯定也不太了解如何将其应用于CGPath的视图...
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)
}
我在屏幕上看不到任何渐变(只有白色背景...)。
最后,在保存状态后,我还尝试了 clip
上下文刚度,但是由于我看不到任何渐变,我不确定是否剪切任何内容(整体视图或只是细分市场)。我阅读了文档,但是并不能解决所有问题(就我而言)。
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...).
推荐答案
cgColor
丢失了。。很抱歉给您带来不便。
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)
这篇关于CAGradient图层在视图上不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!