Skip to content

Instantly share code, notes, and snippets.

@efremidze
Created January 16, 2018 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save efremidze/8c57470d1dd7631648dcbbcca4601a88 to your computer and use it in GitHub Desktop.
Save efremidze/8c57470d1dd7631648dcbbcca4601a88 to your computer and use it in GitHub Desktop.
import UIKit
class RadialGradientLayer: CALayer {
var colors = [CGColor]() { didSet { setNeedsDisplay() } }
var locations: [CGFloat]?
override func draw(in ctx: CGContext) {
ctx.saveGState()
guard let gradient = CGGradient(colorsSpace: nil, colors: colors as CFArray, locations: locations) else { return }
let center = CGPoint(x: bounds.width / 2, y: bounds.height / 2)
ctx.drawRadialGradient(gradient, startCenter: center, startRadius: 0, endCenter: center, endRadius: radius, options: .drawsBeforeStartLocation)
}
}
extension CALayer {
var radius: CGFloat {
return sqrt(pow(bounds.width / 2, 2) + pow(bounds.height / 2, 2))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment