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

后端 未结 4 1073
不思量自难忘°
不思量自难忘° 2020-12-10 05:23

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


(source: ods.com.ua)

相关标签:
4条回答
  • 2020-12-10 05:44

    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.

    0 讨论(0)
  • 2020-12-10 05:58

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

    It's not tested for performance, so beware.

    screenshot

    0 讨论(0)
  • 2020-12-10 06:01

    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).

    0 讨论(0)
  • 2020-12-10 06:06

    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
    

    0 讨论(0)
提交回复
热议问题