CGGradientCreateWithColors

CGGradientRef CGGradientCreateWithColors(CGColorSpaceRef space, CFArrayRef colors, const CGFloat *locations);

// 创建Quartz上下文
let context = UIGraphicsGetCurrentContext()
// 创建色彩空间对象
let colorSpaceRef = CGColorSpaceCreateDeviceRGB()
 // 创建颜色
let redColor = CGColorCreate(colorSpaceRef, [1.0, 0.0, 0.0, 1.0])
let blueColor = CGColorCreate(colorSpaceRef, [0.0, 0.0, 1.0, 1.0])
 // 创建颜色数组
let colorArray = CFArrayCreate(kCFAllocatorDefault, UnsafeMutablePointer<UnsafePointer<Void>>([redColor, blueColor]), 2, nil)
 // 创建渐变对象
let gradientRef = CGGradientCreateWithColors(colorSpaceRef, colorArray, nil)
 CGContextDrawLinearGradient(context, gradientRef, CGPointMake(0.0, 0.0), CGPointMake(0, 200.0), CGGradientDrawingOptions.DrawsBeforeStartLocation)

Creates a CGGradient object from a color space and the provided color objects and locations.
//提供的颜色对象和位置以及颜色空间创建CGGradient对象。
// 参数有:
space
The color space to use for the gradient. You cannot use a pattern or indexed color space.
//用于渐变的颜色空间。您不能使用图案或索引颜色空间
colors
A non-empty array of CGColor objects that should be in the color space specified by space. If space is not NULL, each color will be converted (if necessary) to that color space and the gradient will drawn in that color space. Otherwise, each color will be converted to and drawn in the GenericRGB color space.
//一个非空的CGColor对象数组,应该位于由space指定的颜色空间中。如果space不是NULL,则每种颜色将被转换(如果需要)到该颜色空间,并且渐变将在该颜色空间中绘制。否则,每种颜色都将转换为GenericRGB颜色空间并在其中绘制
locations
The location for each color provided in colors; each location must be a CGFloat value in the range of 0 to 1, inclusive. If 0 and 1 are not in the locations array, Quartz uses the colors provided that are closest to 0 and 1 for those locations.
If locations is NULL, the first color in colors is assigned to location 0, the last color incolors is assigned to location 1, and intervening colors are assigned locations that are at equal intervals in between.
The locations array should contain the same number of items as the colors array.
//每种颜色的位置以颜色提供;每个位置必须是0到1(包括0和1)范围内的CGFloat值。如果0和1不在位置数组中,则Quartz使用提供的颜色最接近0和1的位置。如果位置为NULL,则将颜色中的第一种颜色分配给位置0,将最后颜色的颜色分配给位置1,并为中间颜色分配位于其间的相等间隔的位置。locations数组应包含与colors数组相同数量的项
Returns
A CGGradient object.
例子

+ (UIColor *)randomColor
{
    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

+ (void)drawRect:(CGRect)bounds withParameters:(id<NSObject>)parameters isCancelled:(asdisplaynode_iscancelled_block_t)isCancelledBlock isRasterizing:(BOOL)isRasterizing
{
    CGFloat locations[3];
    NSMutableArray *colors = [NSMutableArray arrayWithCapacity:3];
    [colors addObject:(id)[[RandomCoreGraphicsNode randomColor] CGColor]];
    locations[0] = 0.2;
    [colors addObject:(id)[[RandomCoreGraphicsNode randomColor] CGColor]];
    locations[1] = 1.0;
    [colors addObject:(id)[[RandomCoreGraphicsNode randomColor] CGColor]];
    locations[2] = ( arc4random() % 256 / 256.0 );

    // 1  创建Quartz上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2  创建色彩空间对象
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    // 3 创建渐变对象
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)colors, locations);
    // 4 绘制渐变填充,该渐变填充沿着由提供的起点和终点定义的线变化
    CGContextDrawLinearGradient(ctx, gradient, CGPointZero, CGPointMake(bounds.size.width, bounds.size.height), 0);
    // 5 由CG开头create需要release 释放渐变对象
    CGGradientRelease(gradient);
    // 6 释放色彩空间对象
    CGColorSpaceRelease(colorSpace);
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值