Created
January 16, 2018 06:14
-
-
Save efremidze/8c57470d1dd7631648dcbbcca4601a88 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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