Skip to content

Instantly share code, notes, and snippets.

@victorchee
Created December 1, 2017 09:05
Show Gist options
  • Save victorchee/7ecdffbd8c58d7f7fe7c167b5fa9077d to your computer and use it in GitHub Desktop.
Save victorchee/7ecdffbd8c58d7f7fe7c167b5fa9077d to your computer and use it in GitHub Desktop.
放射渐变
// 放射渐变
CGContextRef context = UIGraphicsGetCurrentContext();
size_t num_locations = 2;
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat components[8] = {1,1,1,1, // 中心颜色(r, g, b, alpha)
.2,.2,.2,1 // 边缘颜色};
CGFloat locations[2] = {0,1};
CGGradientRef gradient = CGGradientCreateWithColorComponents(rgb, components, locations, num_locations);
CGColorSpaceRelease(rgb);
CGRect bounds = self.bounds;
CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));
// 中心发散渐进
CGContextDrawRadialGradient(context, gradient, center, 0, center, bounds.size.width, kCGGradientDrawsAfterEndLocation);
CGGradientRelease(gradient);
// 线形渐变
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.view.frame;
gradientLayer.colors = [NSArray arrayWithObjects:(id)baseColor.CGColor, (id)barColor.CGColor, nil];
[self.view.layer insertSublayer:gradientLayer atIndex:0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment