iOS线性渐变

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()
  // 设置RGB颜色空间
        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
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值