Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save edwardean/287e3b56891eeae1bc29d3a33be5b6fe to your computer and use it in GitHub Desktop.
Save edwardean/287e3b56891eeae1bc29d3a33be5b6fe to your computer and use it in GitHub Desktop.
CGGradientRef
- (void)drawGradientWithContext:(CGContextRef)context colors:(NSArray <id> *)colors path:(CGPathRef)path
aplha:(CGFloat)alpha startPointY:(CGFloat)startY {
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = {0.0, 1.0};
CGContextSaveGState(context);
CGContextAddPath(context, path);
CGContextClip(context);
CGContextSetAlpha(context, alpha);
CGRect pathRect = CGPathGetBoundingBox(path);
CGPoint startPoint = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMinY(pathRect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMaxY(pathRect));
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, kCGGradientDrawsBeforeStartLocation);
CGContextRestoreGState(context);
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment