Circular gradient color "Not linear" in UIView - ios

I have an issue with the gradient color, I'm not able to make the gradient color appear as circular, it always appear like linear, i need the exact gradient in the following image:
PS: this is the code i use:
UIView *view = [[UIView alloc] initWithFrame:self.view.frame];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.startPoint = CGPointMake(0.0,0.4);
gradient.endPoint = CGPointMake(0.0,0.6);
view.layer.masksToBounds = YES;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:153.0/255.0 green:204.0/255.0 blue:0.0/255.0 alpha:1.0] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
[view.layer insertSublayer:gradient atIndex:0];
[self.view addSubview:view];

There is a CGContextDrawRadialGradient in CGContext. I hope it will help you

Related

iOS CAGradientLayer is not working with some colors

I have used below code to set gradient background color of my view.
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = #[(id)[UIColor colorWithRed:44/255 green:58/255 blue:76/255 alpha:1].CGColor, (id)[UIColor colorWithRed:17/255 green:22/255 blue:30/255 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
But its not working. Screen will appear with black color. But when I try to set color like below
gradient.colors = #[(id)[UIColor blueColor].CGColor, (id)[UIColor redColor].CGColor];
then it working fine.I am getting correct output. Can anyone help me to fine why gradient color is not working with UIColor colorWithRed function.
Thanks in advance.
For the original poster's code to work, he just has to divide each color component by 255.0 instead of 255, if not the result is truncated to an int.
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = #[(id)[UIColor colorWithRed:44/255.0 green:58/255.0 blue:76/255.0 alpha:1].CGColor, (id)[UIColor colorWithRed:17/255.0 green:22/255.0 blue:30/255.0 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
Alternatively, make the dividend a float value:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = #[(id)[UIColor colorWithRed:44.0/255 green:58.0/255 blue:76.0/255 alpha:1].CGColor, (id)[UIColor colorWithRed:17.0/255 green:22.0/255 blue:30.0/255 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
Or the most easy to follow solution, make both numbers in the division a float value:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
gradient.colors = #[(id)[UIColor colorWithRed:44.0/255.0 green:58.0/255.0 blue:76.0/255.0 alpha:1].CGColor, (id)[UIColor colorWithRed:17.0/255.0 green:22.0/255.0 blue:30.0/255.0 alpha:1].CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
Try this:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:202.0/255.0 green:107.0/255.0 blue:122.0/255.0 alpha:1] CGColor], (id)[[UIColor colorWithRed:192.0/255.0 green:144.0/255.0 blue:120.0/255.0 alpha:1] CGColor], nil];
gradient.frame = self.view.bounds;
[self.view.layer insertSublayer:gradient atIndex:0];
Try like this!
-(CAGradientLayer *)makeGradientView :(UIColor *)topColor BottomColor:(UIColor *)bottomColor withFrame:(CGRect )rect
{
CAGradientLayer *theViewGradient = [CAGradientLayer layer];
NSArray *gradientLocations = [NSArray arrayWithObjects:[NSNumber numberWithInt:0.0],[NSNumber numberWithInt:1.0], nil];
theViewGradient.locations = gradientLocations;
theViewGradient.colors = [NSArray arrayWithObjects: (id)topColor.CGColor,(id)bottomColor.CGColor,(id)bottomColor.CGColor,(id)bottomColor.CGColor, (id)bottomColor.CGColor, nil];
theViewGradient.frame = rect;
theViewGradient.startPoint = CGPointMake(1,1);
theViewGradient.endPoint = CGPointMake(1.0,0.1);
return theViewGradient;
}
And you can use like this!
#property (weak, nonatomic) IBOutlet UIView *mainView;
CAGradientLayer *theViewGradient = [self makeGradientView:[UIColor colorWithRed:189/255.0 green:227/255.0 blue:106/255.0 alpha:0.5] BottomColor:[UIColor colorWithRed:13/255.0 green:184/255.0 blue:196/255.0 alpha:0.5] withFrame:self.view.bounds];
[_mainView.layer addSublayer:theViewGradient];

Unable to set gradient on uiview using layer

I have set color for uicontrollerview from stroyboard but I want gradient in the background color. So coded the required layer in viewdidappear still its not appearing.
-(void)viewDidAppear:(BOOL)animated{
[self setBackGradient];
}
-(void)setBackGradient{
CAGradientLayer *grad = [CAGradientLayer layer];
grad.frame = self.view.bounds;
UIColor *topColor=[UIColor colorWithRed:0.91 green:0.94 blue:0.99 alpha:1.0];
UIColor *bottomColor=[UIColor colorWithRed:0.67 green:0.80 blue:0.93 alpha:1.0];
NSArray *colorsArr=[[NSArray alloc]initWithObjects:topColor,bottomColor, nil];
grad.colors=colorsArr;
grad.masksToBounds=YES;
[self.view.layer insertSublayer:grad atIndex:0];
}
The colors for a layer need to be CGColors. Try the following:
-(void)setBackGradient{
CAGradientLayer *grad = [CAGradientLayer layer];
grad.frame = self.view.bounds;
NSArray *colorsArr=#[(id)[UIColor colorWithRed:0.91 green:0.94 blue:0.99 alpha:1.0].CGColor, (id)[UIColor colorWithRed:0.67 green:0.80 blue:0.93 alpha:1.0].CGColor];
grad.colors=colorsArr;
grad.masksToBounds=YES;
[self.view.layer insertSublayer:grad atIndex:0];
}

Adding CAGradient layer but unable to get Gradient effect

Here i am using this code to give a gradient effect but not getting the effect at all.
CAGradientLayer *layer2 = [CAGradientLayer layer];
NSArray *gradientColors = [NSArray arrayWithObjects:(id)[self colorWithHexString:#"3eb79d"].CGColor,(id)[self colorWithHexString:#"3fb65c"].CGColor,(id)[self colorWithHexString:#"3cb7da"].CGColor, nil];
[layer2 setColors:gradientColors];
[layer2 setFrame:cell.userBackgroundView.layer.frame];
[cell.userBackgroundView.layer insertSublayer:layer2 atIndex:0];
cell.userBackgroundView.clipsToBounds = YES;
Try this
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame =cell.userBackgroundView.frame;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
[cell.userBackgroundView.layer insertSublayer:gradient atIndex:0];
Try to change index of sublayer from 0 to -1
[cell.userBackgroundView.layer insertSublayer:layer2 atIndex:-1];
Make sure your userBackgroundView is not covered by another view.
Use
[layer2 setFrame:cell.userBackgroundView.bounds];
instead of
[layer2 setFrame:cell.userBackgroundView.layer.frame];
If you are using autoLayout, make sure to set the frame in viewDidAppear or viewDidLayOutSubviews

CAGradientLayer is going out of frame of UILabel

I am doing simple things. As I am learning to implement CAGradientLayer.
The gradient you see is of Label. I am using autoresizing(not Autolayout). Apart from this have no code written. Autoresizing is not a problem since I've not touched that.
CAGradientLayer *gradientLayer = [[CAGradientLayer alloc]init];
gradientLayer.frame = lblHolder.frame;
gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor], (id)[[UIColor greenColor] CGColor], nil];
[lblHolder.layer insertSublayer:gradientLayer atIndex:0];
Try to make the following change:
gradientLayer.frame = lblHolder.bounds;

Apply a blur gradient

I need to mask an image with a blur gradient.
In more detail; I want the picture to start on the left with no blur at all and on its right to be blurred. The blurring will start to occur somewhere half way. I already managed to blur the image in full but as a separate image, but how do I apply a semi-transparent gradient of that blurring?
UIImageView *bluredImgView = [[UIImageView alloc] initWithImage:img];
bluredImgView.frame = frame;
CAGradientLayer *lay = [CAGradientLayer layer];
lay.frame = bluredImgView.bounds;
lay.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite: 0.0 alpha: 0.0] CGColor], (id)[[UIColor colorWithWhite: 1.0 alpha: 1.0] CGColor], nil];
lay.startPoint = CGPointMake(0.0f, 0.0f);
lay.endPoint = CGPointMake(1.0f, 0.0f);
bluredImgView.layer.mask = lay;
[_profileImageView addSubview:bluredImgView];
This question is rather old but still.. None of the previous answers combined the blur effect to a gradient.
This adds the effect from bottom to top (looks cool for title images):
titleImage.image = [UIImage imageNamed: titleImage];
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *view = [[UIVisualEffectView alloc] initWithEffect:effect];
view.frame = titleImage.frame;
UIImageView *bluredImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_location.titleImage]];
bluredImgView.frame = _titleImage.frame;
CAGradientLayer *lay = [CAGradientLayer layer];
lay.frame = bluredImgView.bounds;
lay.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite: 0.0 alpha: 0.0] CGColor], (id)[[UIColor colorWithWhite: 0.0 alpha: 1.0] CGColor], nil];
lay.startPoint = CGPointMake(0.0f, 0.0f);
lay.endPoint = CGPointMake(0.0f, 1.0f);
view.layer.mask = lay;
[titleImage addSubview:view];
Add the blurred image as a separate layer on top of your image view's layer. Then create a CAGradientLayer and add it as a mask layer on your blurred image layer.
Use code below:
NSArray *colors = [NSArray arrayWithObjects:(__bridge id)[[UIColor colorWithWhite:0 alpha:0] CGColor], (__bridge id)[[UIColor colorWithWhite:0 alpha:1] CGColor], nil];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
[gradientLayer setFrame:[bluredImgView bounds]];
[gradientLayer setColors:colors];
[gradientLayer setStartPoint:CGPointMake(0.0f, 0.0f)];
[gradientLayer setEndPoint:CGPointMake(1.0f, 0.0f)];
bluredImgView.layer.mask = gradientLayer;

Resources