iOS 渐变
渐变动画链接
部分渐变
1.线性渐变
static func linear(colors: [CUnsignedInt], locations: [CGFloat], fromP: CGPoint = CGPoint.init(x: 0, y: 0), toP: CGPoint = CGPoint.init(x: 0, y: 01)) -> UIImage? {
let frame = CGRect.init(x: 0, y: 0, width: 20, height: 20)
UIGraphicsBeginImageContextWithOptions(frame.size, true, UIScreen.main.scale)
let context = UIGraphicsGetCurrentContext()
let rgb = CGColorSpaceCreateDeviceRGB()
context?.scaleBy(x: frame.size.width, y: frame.size.height)
var components: [CGFloat] = []
for (_, color) in colors.enumerated() {
components.append(contentsOf: [CGFloat(Double(((color & 0xff0000) >> 16)) / 255.0), CGFloat(Double(((color & 0xff00) >> 8)) / 255.0), CGFloat(Double((color & 0xff)) / 255.0), CGFloat(1)])
}
if let gradient = CGGradient.init(colorSpace: rgb, colorComponents: components, locations: locations, count: locations.count) {
context?.drawLinearGradient(gradient, start: fromP, end: toP, options: .drawsAfterEndLocation)
}
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}