美文网首页
颜色渐变

颜色渐变

作者: AlanGit | 来源:发表于2019-06-10 18:21 被阅读0次
 public class func gradientColor(_ startPoint: CGPoint, endPoint: CGPoint, frame: CGRect, colors: [UIColor]) -> UIColor? {
        // init a CAGradientLayer and set its frame
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = frame
        
        // turn the array of UIColor's into an array of CGColor's
        let cgColors = colors.map({$0.cgColor})
        
        // set the colors of the gradient
        gradientLayer.colors = cgColors
        
        // set the start and end points of the gradient
        gradientLayer.startPoint = startPoint
        gradientLayer.endPoint = endPoint
        
        // start an image context
        UIGraphicsBeginImageContextWithOptions(gradientLayer.bounds.size, false, UIScreen.main.scale)
        
        // draw the gradient layer in the context
        gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
        
        // get the image of the gradient from the current image context
        let gradientImage = UIGraphicsGetImageFromCurrentImageContext()
        
        // end the context
        UIGraphicsEndImageContext()
        
        // return a new UIColor using the gradient image we made
        return UIColor(patternImage: gradientImage!)
    }

 public class func radialGradientColor(_ frame: CGRect, colors: [UIColor]) -> UIColor? {
        // start the image context
        UIGraphicsBeginImageContextWithOptions(frame.size, false, UIScreen.main.scale)
        
        // get an array of CGColor's from the UIColor's
        let cgColors = colors.map({$0.cgColor})
        
        // init a color space
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        
        // get a CFArrayRef from our array of CGColor's
        let arrayRef = cgColors as CFArray
        
        // init the gradient
        let gradient = CGGradient(colorsSpace: colorSpace, colors: arrayRef, locations: nil)
        
        // make the center point in the center
        let centrePoint = CGPoint(x: frame.size.width/2, y: frame.size.height/2)
        
        // calculate the radius from the frame
        let radius = max(frame.size.width, frame.size.height)/2
        
        // draw the radial gradient
        UIGraphicsGetCurrentContext()?.drawRadialGradient(gradient!,
                                     startCenter: centrePoint,
                                     startRadius: 0,
                                     endCenter: centrePoint,
                                     endRadius: radius,
                                     options: .drawsAfterEndLocation)
        
        // get a UIImage from the current context
        let gradientImage = UIGraphicsGetImageFromCurrentImageContext()
        
        // return a new UIColor from the radial gradient we just made
        return UIColor(patternImage: gradientImage!)
    }

相关文章

  • 颜色渐变/渐变高亮/背景颜色渐变

    background: linear-gradient(to right, #ff6034, #ee0a24); ...

  • 7.2.2 实战:用实色渐变制作水晶按钮

    1、选择渐变颜色,渐变工具 2、打开渐变编辑器 3、调整渐变颜色,拖动色标,改变颜色位置 4、拖动动点改变颜色的混...

  • 颜色渐变

    var mesh = creatGradPlane(0,0,0, 100,100, [[0, '#fcc'], [...

  • 颜色渐变

    颜色透明渐变效果 参考链接:UIView 的渐变色呈现CAGradientLayer的一些属性解析

  • 颜色渐变

    线性渐变:background : linear-gradient(起始颜色到结束颜色) 还可以指定角度 默认是从...

  • 颜色渐变

  • 颜色渐变

    在view的layer上添加一个渐变的layer 渐变过程加一个动画

  • CAGradientLayer

    1.startPoint 和 endPoint 颜色渐变方向(从左到右渐变) 2. locations 颜色渐变分...

  • 6.3.2 实战:用渐变填充图层制作蔚蓝晴空

    1、用快速选用工具,选取天空 2、新建一个渐变填充调整图层 3、调整渐变颜色 渐变颜色左 渐变颜色右 4、新建图层...

  • AI常用知识

    渐变颜色: 首先用选择工具选择图形,选择渐变工具,双击渐变工具打开渐变窗口,点击左上角色块启用渐变颜色,通...

网友评论

      本文标题:颜色渐变

      本文链接:https://www.haomeiwen.com/subject/xnjtfctx.html