How to make a conical gradient in iOS using Core Graphics / Quartz 2D?
Asked Answered
A

4

13

How can I draw such a conical gradient in iOS using Core Graphics / Quartz 2D API?

Conical Gradient Sample
(source: ods.com.ua)

Avian answered 21/3, 2011 at 14:41 Comment(0)
B
17

If anyone is still looking for a solution, Apple finally introduced .conic gradient type in iOS 12. Perfect for masking to create circular progress bar with gradient.

Example:

let gradientLayer = CAGradientLayer()
gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 0.5, y: 0)
gradientLayer.type = .conic
gradientLayer.colors = [UIColor.red.cgColor, UIColor.orange.cgColor, UIColor.green.cgColor]
gradientLayer.frame = bounds

conic_gradient

Bison answered 7/12, 2018 at 7:45 Comment(2)
As .conic available from iOS 12 how we can achieve this in previous version(iOS 11)Haircloth
You have to look for other solutions proposed in this question.Spermato
U
6

Recently I've made a custom CALayer class for this: AngleGradientLayer

It's not tested for performance, so beware.

screenshot

Upanishad answered 7/3, 2012 at 11:11 Comment(0)
M
3

I wanted pure Swift solution for this, and it was also kind of a challenging task for me.

At the end, I wrote AEConicalGradient which does that with interesting approach (by using a circle and drawing lines of different colors to the center).

AEConicalGradient

Mitchelmitchell answered 9/11, 2015 at 15:1 Comment(0)
U
1

There is no Quartz function for this style of gradient. Unless you're ready to dig into mathematics behind it, I'd suggest you use pre-made images for this. It's not a problem if you need it only for opacity mask.

Upanishad answered 27/3, 2011 at 12:53 Comment(2)
It's very easy to do if you have to support <12, but in fact, they added it to 12 so it's now included in iOSMetabolism
Where have they added it to CoreGraphcs? I only see .conic in CALayer, but CoreGraphics doesn't render .conic only radial and linear.Atomize

© 2022 - 2024 — McMap. All rights reserved.