123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- //
- // IHRadialGradientLayer.swift
- // Inhealth
- //
- // Created by weclouds on 2020/4/17.
- // Copyright © 2020 weclouds. All rights reserved.
- //
- import UIKit
- class IHRadialGradientLayer: CALayer {
- var center : CGPoint {
- return CGPoint(x: bounds.width / 2, y: bounds.height / 2)
- }
-
- var radius :CGFloat {
- return (bounds.width + bounds.height) / 2
- }
-
- var colors : [UIColor] = [UIColor.purple,UIColor.white]{
- didSet{
- setNeedsDisplay()
- }
- }
- var cgColors : [CGColor] {
- return colors.map { (color) -> CGColor in
- return color.cgColor
- }
- }
- override init() {
- super.init()
- needsDisplayOnBoundsChange = false
- }
-
-
- required init?(coder: NSCoder) {
- super.init()
- }
-
- override func draw(in ctx: CGContext) {
- ctx.saveGState()
- let colorSpace = CGColorSpaceCreateDeviceRGB()
- let locations :[CGFloat] = [0.0,1.0]
- let gradient = CGGradient(colorsSpace: colorSpace, colors: cgColors as CFArray, locations: locations)
-
- ctx.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: radius, options: CGGradientDrawingOptions(rawValue: 0))
- }
- }
|