IHGradientLayer.swift 1018 B

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // IHGradientLayer.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/4/29.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class IHGradientLayer: CALayer {
  10. var colors:[CGColor]?{
  11. didSet{
  12. setNeedsDisplay()
  13. }
  14. }
  15. override init() {
  16. super.init()
  17. }
  18. required init?(coder: NSCoder) {
  19. fatalError("init(coder:) has not been implemented")
  20. }
  21. override func draw(in ctx: CGContext) {
  22. if let colors = self.colors{
  23. let endRadius = sqrt(pow(frame.width/2, 2) + pow(frame.height/2, 2))
  24. let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
  25. let gradient = CGGradient(colorsSpace: nil, colors: colors as CFArray, locations: nil)
  26. ctx.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: endRadius, options: CGGradientDrawingOptions.drawsBeforeStartLocation)
  27. }
  28. }
  29. }