Created
June 7, 2017 02:57
-
-
Save edwardean/287e3b56891eeae1bc29d3a33be5b6fe to your computer and use it in GitHub Desktop.
CGGradientRef
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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