How can I use a CAGradientLayer to most efficiently draw a gradient around a circle / with angles?
I made the one underneath with the help of this project. It uses a bitmap context for drawing but a CAGradientLayer would be far more efficient.
Unfortunately I could only figure out how to make linear gradients with it.
I realize many years have passed since this question was asked, but for anybody who stumbles across it now, iOS 12 added a conic gradient type that makes this possible.
gradientLayer.type = CAGradientLayerType.conic
gradientLayer.frame = bounds
gradientLayer.colors = [startColor.cgColor, endColor.cgColor]
gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 0.5, y: 0)
Gradient layers currently only support linear gradients. However, if you look at the interface for gradient layers, it includes a type property. Right now the only type defined is kCAGradientLayerAxial (linear).
The fact that there is a type property suggests that Apple will be adding more types at some future date, and radial gradients seem like a very like addition.
You might look at creating your own custom subclass of CAGradientLayer that draws radial gradients as well as linear. I've seen demo projects on the net that create custom CALayer subclasses.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With