I know this post is a bit outdated, but I thought it might help someone.
Use this code to generate a radial gradient background:
- (UIImage *)radialGradientImage:(CGSize)size start:(float)start end:(float)end centre:(CGPoint)centre radius:(float)radius{ UIGraphicsBeginImageContextWithOptions(size, YES, 1); size_t count = 2; CGFloat locations[2] = {0.0, 1.0}; CGFloat components[8] = {start, start, start, 1.0, end, end, end, 1.0}; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGGradientRef grad = CGGradientCreateWithColorComponents (colorSpace, components, locations, count); CGColorSpaceRelease(colorSpace); CGContextDrawRadialGradient (UIGraphicsGetCurrentContext(), grad, centre, 0, centre, radius, kCGGradientDrawsAfterEndLocation); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); CGGradientRelease(grad); UIGraphicsEndImageContext(); return image;}
Define the gradient in the .h file like this:
UIImageView *gradient;
Name your gradient as follows:
- (void)addGradient{ CGSize size = self.view.bounds.size; CGPoint centre = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2); float startColor = 1.0f; float endColor = 0.0f; float radius = MIN(self.view.bounds.size.width/4, self.view.bounds.size.height/4); gradient = [[UIImageView alloc] initWithImage:[self radialGradientImage:size start:startColor end:endColor centre:centre radius:radius]]; [gradient setBackgroundColor:[UIColor clearColor]]; [gradient setUserInteractionEnabled:YES]; [gradient setAlpha:0.6f]; [self.view addSubview:gradient];}
source share