怎么给view添加渐变色:
首先我们来创建一个View的扩展
/**
类似 OC 中 UIView 的类别(分类)
*/
import Foundation
import UIKit
extension UIView {
///设置渐变色
//Colors:渐变色色值数组
func setLayerColor(_ Colors:[CGColor]) {
let layer = CAGradientLayer()
layer.frame = self.bounds
layer.colors = Colors
layer.startPoint = CGPoint(x: 0, y: 0)
layer.endPoint = CGPoint(x: 1, y: 0)
self.layer.addSublayer(layer)
}
}