Creating gradients with Core Graphics in iOS - ios

I have the following code to create a gradient (or start of one):
CAGradientLayer *gradient = [CAGradientLayer layer];
UIColor *lightGreen = [UIColor colorWithRed:66.0f/255.0f green:79.0f/255.0f blue:91.0f/255.0f alpha:1.0f];
UIColor *darkGreen = [UIColor colorWithRed:66.0f/255.0f green:79.0f/255.0f blue:91.0f/255.0f alpha:1.0f];
Why does this line give me "Expected identifier"?
gradient.colors = [NSArray arrayWithObjects:(id)[lightGreen.CGColor]];

You have to many [ in your code and you are not closing with , nil:
gradient.colors = [NSArray arrayWithObjects:(id)[lightGreen.CGColor]];
Should be:
gradient.colors = [NSArray arrayWithObjects:(id)lightGreen.CGColor, nil];
Or even:
gradient.colors = #[(id)lightGreen.CGColor];

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

ios - set gradient with custom colors

I am trying to set gradient on my main View with custom colors. View is displayed completely white though. What's wrong with this record?
CAGradientLayer *gradient = [CAGradientLayer new];
gradient.colors = #[(id)[UIColor colorWithRed:92.0 green:196.0 blue:244.0 alpha:1].CGColor,
(id)[UIColor colorWithRed:47.0 green:146.0 blue:229.0 alpha:1].CGColor];
gradient.frame = self.backGroundView.bounds;
gradient.locations = #[#0.5, #0.5];
[self.backGroundView.layer insertSublayer:gradient atIndex:0];
This is properly displayed with blue and red cut in half of view's height.
CAGradientLayer *gradient = [CAGradientLayer new];
gradient.colors = #[(id)[UIColor redColor].CGColor, (id)[UIColor blueColor].CGColor];
gradient.frame = self.backGroundView.bounds;
gradient.locations = #[#0.5, #0.5];
[self.backGroundView.layer insertSublayer:gradient atIndex:0];
your colors are wrong. the UIColor initializer expects values between 0 and 1. try this:
gradient.colors = #[(id)[UIColor colorWithRed:92/255.0 green:196/255.0 blue:244/255.0 alpha:1].CGColor,
(id)[UIColor colorWithRed:47/255.0 green:146/255.0 blue:229/255.0 alpha:1].CGColor];

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

How to create a gradient

I have this code to create a gradient background
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
UIColor *startColour = [UIColor colorWithHexString:#"#bcdef6"];
UIColor *endColour = [UIColor colorWithHexString:#"#fad7e6"];
UIColor *middleColor = [UIColor colorWithHexString:#"#ffffff"];
gradient.colors = [NSArray arrayWithObjects:(id)[startColour CGColor],(id)middleColor, (id)[endColour CGColor], nil];
gradient.endPoint = CGPointMake(1, 0);
gradient.startPoint = CGPointMake(0, 1);
[self.layer insertSublayer:gradient atIndex:0];
how do i get rid of the greyish effect in the middle ?
this is the desired result:
this is the actual result:
I can see a bug with your colors array, you have not taken the CGColor of the middle color (white). By changing the array to this, I can get your desired result.
gradient.colors = #[(id)startColour.CGColor, (id)middleColor.CGColor, (id)endColour.CGColor];
By playing around with the gradient locations, you can also see more of the blue and pink colors should that create a better effect.
gradient.locations = #[#0.2f, #0.5f, #0.8f];

CAGradientLayer for different views

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

Resources