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];
Related
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];
I need to do a colour shading in objective c. As shown in this image shade is on the button of HardService and SoftService.
Is there is any way to do this ? Thanks in Advance!
CAGradientLayer would server your purpose - Reference:
http://blog.apoorvmote.com/gradient-background-uiview-ios-swift/
You can use either a gradient image or CAGradientLayer for this purpose
CAGradientLayer added to an existing button:
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = button.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
[button.layer insertSublayer:gradient atIndex:0];
You can use an image for showing shadow. Use blur image or change alpha of the imageview.
Or if you want to do it programmatically, try it:
Obj c:
//create elliptical shadow for button through UIBezierPath
CGRect ovalRect = button.bounds;
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:ovalRect];
//applying shadow to path
button.layer.shadowColor = [UIColor yourcolor].CGColor;
button.layer.shadowOffset = CGSizeMake(0.0, 0.0);
button.layer.shadowOpacity = 1.0;
button.layer.shadowRadius = 3.0;
button.layer.shadowPath = path.CGPath;
Swift :
//create elliptical shdow forimage through UIBezierPath
var ovalRect = button.bounds
var path = UIBezierPath(ovalInRect: ovalRect)
//applying shadow to path
button.layer.shadowColor = UIColor(white: 0.0, alpha: 0.5).CGColor
button.layer.shadowOffset = CGSizeMake(0.0, 0.0)
button.layer.shadowOpacity = 1.0
button.layer.shadowRadius = 3.0
button.layer.shadowPath = path.CGPath
Taken from http://www.innofied.com/implementing-shadow-ios/ and also have a look to know more: UIView with rounded corners and drop shadow?
My another answer related to this: Drop shadow in ios
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];
}
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
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];