// // CAGradientLayer+Extension.swift // QuickSearchLocation // // Created by mac on 2024/4/11. // import UIKit public enum ViewGradientDirection { /// 水平从左到右 case horizontal /// 垂直从上到下 case vertical public func point() -> (CGPoint, CGPoint) { switch self { case .horizontal: return (CGPoint(x: 0, y: 0), CGPoint(x: 1, y: 0)) case .vertical: return (CGPoint(x: 0, y: 0), CGPoint(x: 0, y: 1)) } } } public extension CAGradientLayer { // MARK: 1.1、设置渐变色图层 /// 设置渐变色图层 /// - Parameters: /// - direction: 渐变方向 /// - gradientColors: 渐变的颜色数组(颜色的数组) /// - gradientLocations: 设置渐变颜色的终止位置,这些值必须是递增的,数组的长度和 colors 的长度最好一致 func gradientLayer(_ direction: ViewGradientDirection = .horizontal, _ gradientColors: [Any], _ gradientLocations: [NSNumber]? = nil, _ transform: CATransform3D? = nil) -> CAGradientLayer { // 设置渐变的颜色数组 self.colors = gradientColors // 设置渐变颜色的终止位置,这些值必须是递增的,数组的长度和 colors 的长度最好一致 self.locations = gradientLocations // 设置渲染的起始结束位置(渐变方向设置) self.startPoint = direction.point().0 self.endPoint = direction.point().1 if let weakTransform = transform { self.transform = weakTransform } return self } }