iOS颜色渐变


方法1:

 //图片边缘模糊

        let colorLayer =CAGradientLayer()

        colorLayer.frame =circleImageIcon.frame

        colorLayer.position =circleImageIcon.center

        //设置渐变颜色及透明度

        colorLayer.colors = [UIColor(red:250 /255.0, green:250 /255.0, blue:250 /255.0, alpha:0.1).CGColor,UIColor(red:250 /255.0, green:250 /255.0, blue:250 /255.0, alpha:0.5).CGColor,UIColor(red:250 /255.0, green:250 /255.0, blue:250 /255.0, alpha:1.0).CGColor]

        //颜色分割线

        colorLayer.locations = [0.5,0.75]

        //颜色起止点

        colorLayer.startPoint =CGPointMake(0.5,0)

        colorLayer.endPoint =CGPointMake(1,0)

        circleImageIcon.layer.insertSublayer(colorLayer, atIndex:0)

方法2

新建一个类继承于UIVIew,重写drawRect方法,

 //获取上下文

        let context =UIGraphicsGetCurrentContext()

//        let gradient: CGGradientRef

        let colorSpace =CGColorSpaceCreateDeviceRGB()

        //颜色渐变组件,一组四个数字分别对应rgb透明度

        let componets:[CGFloat] = [220.0,220.0220.0,0.1,220.0,220.0,220.01]

        //定义渐变位置

        let locations:[CGFloat] = [0,1]

        let gradient =CGGradientCreateWithColorComponents(colorSpace, componets, locations,2)!

        //绘制线性渐变

        CGContextDrawLinearGradient(context, gradient,CGPointMake(0, rect.size.height),CGPointMake(rect.size.width, rect.size.height),CGGradientDrawingOptions.DrawsAfterEndLocation)


你可能感兴趣的:(iOS颜色渐变)