// // IHGradientLayer.swift // Inhealth // // Created by weclouds on 2020/4/29. // Copyright © 2020 weclouds. All rights reserved. // import UIKit class IHGradientLayer: CALayer { var colors:[CGColor]?{ didSet{ setNeedsDisplay() } } override init() { super.init() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func draw(in ctx: CGContext) { if let colors = self.colors{ let endRadius = sqrt(pow(frame.width/2, 2) + pow(frame.height/2, 2)) let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2) let gradient = CGGradient(colorsSpace: nil, colors: colors as CFArray, locations: nil) ctx.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: endRadius, options: CGGradientDrawingOptions.drawsBeforeStartLocation) } } }