绘制渐变效果

        CGRect lineRect = CGRectMake(0 0, 3, 100);
        
        CGPoint startLinePoint = CGPointZero;
        CGPoint endLinePoint = CGPointZero;
        startLinePoint = CGPointMake(lineRect.origin.x, CGRectGetMaxY(drawRect));
        endLinePoint = CGPointMake(lineRect.origin.x, rect.size.height);
        
        CGFloat red = 0.0;
        CGFloat green = 0.0;
        CGFloat blue = 0.0;
        CGFloat alpha = 0.0;
        [uiColor getRed:&red green:&green blue:&blue alpha:&alpha];
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

// 画多个,需要先保存一下上下文,画完需要还原,不然裁剪之后只有lineRect部分了
CGContextSaveGState(context);

image.png
        CGFloat components[] = { red, green, blue, alpha, 1.0, 1.0, 1.0, 0.3};
        CGFloat locations[] = { 0.0, 1.0};
        CGGradientRef gradient = CGGradientCreateWithColorComponents( colorSpace, components, locations, 2);
        CGContextClipToRect(context, lineRect);
image.png
        CGContextDrawLinearGradient(context, gradient, startLinePoint, endLinePoint, kCGGradientDrawsAfterEndLocation);
        CGContextRestoreGState(context);
        CGGradientRelease(gradient);
        CGColorSpaceRelease(colorSpace);

你可能感兴趣的:(绘制渐变效果)