CAGradientLayer for different views - ios

I would like to create a color gradient with CAGradientLayer for multiple views with different sizes. I don't know how to define the frame separately:
UIColor *darkOp = [UIColor colorWithRed:0.2f green:0.2f blue:0.27f alpha:1.0];
UIColor *lightOp = [UIColor colorWithRed:0.36f green:0.35f blue:0.42f alpha:1.0];
// Create the gradient
CAGradientLayer *gradient = [CAGradientLayer layer];
// Set colors
gradient.colors = [NSArray arrayWithObjects:
(id)darkOp.CGColor,
(id)lightOp.CGColor,
nil];
//set radius
gradient.cornerRadius = 5.0;
// Set bounds BUT just for one view size
gradient.frame = self.numberRegionView.bounds; //<-- here I can just define one frame size
// Add the gradient to one view
[self.numberRegionView.layer insertSublayer:gradient atIndex:0];
//but how to add the gradient layer to views with different sizes ???
//[self.graphRegionView.layer insertSublayer:gradient atIndex:0]; ???
//[self.barRegionView.layer insertSublayer:gradient atIndex:0]; ???
Thanks!

-(void)setGradientForView:(UIView*)view
{
static UIColor *darkOp = [UIColor colorWithRed:0.2f green:0.2f blue:0.27f alpha:1.0];
static UIColor *lightOp = [UIColor colorWithRed:0.36f green:0.35f blue:0.42f alpha:1.0];
// Create the gradient
CAGradientLayer *gradient = [CAGradientLayer layer];
// Set colors
gradient.colors = [NSArray arrayWithObjects:
(id)darkOp.CGColor,
(id)lightOp.CGColor,
nil];
//set radius
gradient.cornerRadius = 5.0;
// Set bounds BUT just for one view size
gradient.frame = view.bounds; //<-- here I can just define one frame size
// Add the gradient to one view
[view.layer insertSublayer:gradient atIndex:0];
}
Then use this code for your three views:
[self setGradientForView:self.numberRegionView];
[self setGradientForView:self.barRegionView];
[self setGradientForView:self.numberRegionView];

Related

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];
}

Button title goes behind gradient layer

I know the fact that if we add the title after assigning the gradient to the button as sublayer, button's title will be in front.
But this case is different.
I am changing gradient for normal state and touched down states of button.
Here is my code :
-(void) buttonClicked:(UIButton*)sender{
sender.backgroundColor = [UIColor clearColor];
// Finding old gradient layer to remove it
for (CALayer *layer in [sender.layer.sublayers copy]){
if ([[layer name] isEqualToString:#"gradientLayer"]) {
[layer removeFromSuperlayer];
}
}
// Applying the new gradient
UIColor *colorTwo = [UIColor colorWithRed:1.00 green:0.81 blue:0.00 alpha:0.95]; // Original
UIColor *colorThree = [[UIColor brownColor] colorWithAlphaComponent:1];
UIColor *colorFour = [[UIColor blackColor] colorWithAlphaComponent:0.9];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = sender.layer.bounds;
gradientLayer.colors = [NSArray arrayWithObjects: (id)[[UIColor whiteColor] colorWithAlphaComponent:0.5].CGColor,(id) colorTwo.CGColor,(id) colorThree.CGColor,(id) colorFour.CGColor, nil];
gradientLayer.locations = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:1.0f], nil];
sender.layer.cornerRadius = 5;
gradientLayer.cornerRadius = sender.layer.cornerRadius;
// Adding gradient
[sender.layer addSublayer:gradientLayer];
gradientLayer.name = #"gradientLayer";
// Adding title after applying the gradient ( BUT ALSO TITLE IS GOING BEHIND THE GRADIENT )
sender.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
sender.titleLabel.numberOfLines = 2;
NSString *str = sender.titleLabel.text;
[sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[sender setTitle:str forState:UIControlStateNormal];
}
-(void) touchedDown:(UIButton*)sender{
// Finding old gradient layer to remove it
for (CALayer *layer in [sender.layer.sublayers copy]){
if ([[layer name] isEqualToString:#"gradientLayer"]) {
[layer removeFromSuperlayer];
}
}
// Applying the new gradient
CAGradientLayer *gradientLayer1 = [CAGradientLayer layer];
gradientLayer1.frame = sender.layer.bounds;
gradientLayer1.colors = [NSArray arrayWithObjects: (id)[[UIColor whiteColor] colorWithAlphaComponent:0.5].CGColor, (id)[UIColor colorWithWhite:0.4f alpha:0.2f].CGColor,(id)[UIColor colorWithWhite:0.4f alpha:0.2f].CGColor, nil];
gradientLayer1.locations = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:1.0f], nil];
gradientLayer1.cornerRadius = sender.layer.cornerRadius;
// Adding Gradient
[sender.layer addSublayer:gradientLayer1];
gradientLayer1.name = #"gradientLayer";
sender.backgroundColor = [UIColor blackColor];
[sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
At first time when i launch the app, title is at front. But when i start clicking it, then the title goes behind the gradient. Kindly help me with this problem. Thanks for the time (:
The problem is this line:
[sender.layer addSublayer:gradientLayer];
That means: add the gradient layer in front of all other sublayers. That includes the layer that displays the title label.
Instead, call insertSublayer:atIndex: (or above: or below:) to position the layer among the other layers where you want it.
However, it would be even better not to mess with the button's layers directly at all. Instead of applying the gradient as a layer, draw the gradient (in code) into an image and use that image as the button's background image.
Moreover, you can configure the background image to change automatically when the user is tapping the button.

How to make CALayer border with Gradient or Multiple Colors?

CALayer *rightBorder = [CALayer layer];
rightBorder.borderColor = [UIColor redColor].CGColor;
rightBorder.borderWidth = 1;
rightBorder.frame = CGRectMake(-1, -1, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)+2);
[self.layer addSublayer:rightBorder];
I am making border of a WebView like above. here self is inherited from UIWebView.
How can I change
rightBorder.borderColor = [UIColor redColor].CGColor;
to a Gradient color, so half of my color should be blue, and half white.
Here, I am applying on RED color to border of my WebView. However I want a multicolor (2 color) or gradient.
Thanks.
To show different color in one layer with gradient effect i am sharing one method of my code you have to use CAGradientLayer for that,
-(void)addGradiantColor:(UIView *)view;
{
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = view.frame;
// gradientLayer.startPoint = CGPointMake(0.0,0.0);
// gradientLayer.endPoint = CGPointMake(1.0,1.0);
NSMutableArray *colors = [NSMutableArray array];
[colors addObject:(id)[[UIColor colorWithRed:134.0/255.0 green: 234.0/255.0 blue:63.0/255.0 alpha:1.0] CGColor]];
[colors addObject:(id)[[UIColor colorWithRed:215.0/255.0 green: 82.0/255.0 blue:76.0/255.0 alpha:1.0] CGColor]];
gradientLayer.colors = colors;
[view.layer addSublayer:gradientLayer];
}

Circular gradient color "Not linear" in UIView

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

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