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
Related
Does anyone know how I can convert a box-shadow to a shadow on a UIButton in Objective-C?
For example this shadow:
box-shadow: 2px 0 0 0 #46d466,-2px 0 0 0 #46d466,0 2px 4px 0 rgba(0,0,0,.1),0 2px 8px 0 rgba(70,212,102,.7);
UIButton have a special property layer of type CALayer.The layer property is great for many quick special effects, some built-in, like shadows are one of those built-in effects.
Please find the below code.
btn.layer.shadowColor = [UIColor grayColor].CGColor;
btn.layer.shadowOffset = CGSizeMake(3.0, 3.0);
btn.layer.shadowOpacity = 2.0;
btn.layer.shadowRadius = 2.0;
shadowOffset : The blur radius used to create the shadow. Defaults
to (0, -3).
shadowOpacity : The opacity of the layer’s shadow. Defaults to 0.
shadowRadius : The blur radius (in points) used to render the
layer’s shadow.
For more information please review the API on developer site.
Or also you can change the offset of shadow.
btn.layer.shadowOffset = CGSizeMake(10.0, 10.0);
You can also change the radius of the shadow which affects how blurry the shadow appears.
btn.layer.shadowRadius = 10.0;
Hope it will work for you!!!
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = CGRectMake( 0, 0, 5, testBtn.frame.size.height);
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor clearColor].CGColor, (id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0.8f].CGColor, nil];
[testBtn.layer insertSublayer:gradient atIndex:0];
you can give whatever color or gradient.
hope this helps.
or
CALayer * bgLayer = [CALayer layer];
bgLayer.frame = CGRectMake( 0, 0, 5, testBtn.frame.size.height);
bgLayer.backgroundColor = [UIColor grayColor].CGColor;
[testBtn.layer insertSublayer: bgLayer atIndex:0];
You need to use the button layer property. You can only assign one colour though (or use a gradient as suggested above) e.g.
button.layer.shadowColor = [UIColor grayColor].CGColor;
button.layer.shadowOffset = CGSizeMake(0, 1.0);
button.layer.shadowOpacity = 1.0;
button.layer.shadowRadius = 0.0;
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 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];
I've been trying for some time now, using CAGradientLayer to produce this .
Initially I tried having a gradient background using the .colors property, however this is only a background fill. Trying this approach, I tried to add another CALayer inside that had a black background, however i could never get the radius correct, and it would create a line of various thickness at the rounded corners.
Is there a better way to create this rounded rect border with a gradient to it? Will CALayer not achieve this and should I move over to UIBezierPath or CGContextRef?
Code for failed attempt:
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(12*twelthWidth - squareCentre.x - squareSize.width, squareCentre.y, squareSize.width, squareSize.height)];
// Create the rounded layer, and mask it using the rounded mask layer
CAGradientLayer *roundedLayer = [CAGradientLayer layer];
[roundedLayer setFrame:view.bounds];
roundedLayer.cornerRadius = view.bounds.size.width/5;
roundedLayer.masksToBounds = YES;
roundedLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor], (id)[[UIColor blueColor] CGColor], nil];
roundedLayer.borderWidth = 4;
roundedLayer.borderColor = [UIColor clearColor].CGColor;
// Add these two layers as sublayers to the view
int BorderWidth = 5;
CALayer *solidColour = [CALayer layer];
solidColour.cornerRadius = view.bounds.size.width/5;
solidColour.masksToBounds = YES;
solidColour.backgroundColor = [UIColor blackColor].CGColor;
[solidColour setFrame:CGRectMake(BorderWidth, BorderWidth, roundedLayer.bounds.size.width - 2*BorderWidth, roundedLayer.bounds.size.height - 2*BorderWidth)];
[view.layer insertSublayer:roundedLayer atIndex:0];
[view.layer insertSublayer:solidColour above:roundedLayer];
[self.view addSubview:view];
Which produces:
Whereby the corners aren't right. Could it be that I need to calculate a different radius for the second layer?
Edit
After setting the radius of the solid colour to solidColour.bounds.size.width/5, it still isn't right - It goes too thin at the corners.
The problem you are seeing is because the inner and outer corner radius are the same. That is what causes the line thickness to vary. This illustration from CSS-Tricks highlights the issue (even thought you aren't using CSS, the problem is still the same):
The solution is to calculate the inner radius as:
innerRadis = outerRadius - lineThickness
As shown in this illustration by Joshua Hibbert:
Instead of using the view bounds to set the cornerRadius of the solid layer, use the layer's frame value after you've set it to the correct inset value:
solidColour.masksToBounds = YES;
solidColour.backgroundColor = [UIColor blackColor].CGColor;
[solidColour setFrame:CGRectMake(BorderWidth, BorderWidth, roundedLayer.bounds.size.width - 2*BorderWidth, roundedLayer.bounds.size.height - 2*BorderWidth)];
solidColour.cornerRadius = solidColour.frame.size.width/5;
[view.layer insertSublayer:roundedLayer atIndex:0];
[view.layer insertSublayer:solidColour above:roundedLayer];
With this I get the following image
Just for your convenience if you use swift
let v = yourUIVIEW
let outerRadius:CGFloat = 60
let borderWidth:CGFloat = 8;
let roundedLayer = CAGradientLayer()
roundedLayer.frame = v.bounds
roundedLayer.cornerRadius = outerRadius
roundedLayer.masksToBounds = true
roundedLayer.endPoint = CGPoint(x: 1 , y: 0.5)
roundedLayer.colors = [ UIColor.redColor().CGColor, UIColor.blueColor()!.CGColor]
let innerRadius = outerRadius - borderWidth
//Solid layer
let solidLayer = CALayer()
solidLayer.masksToBounds = true
solidLayer.backgroundColor = UIColor.whiteColor().CGColor
solidLayer.cornerRadius = innerRadius
solidLayer.frame = CGRect(x: borderWidth, y: borderWidth, width: roundedLayer.bounds.width - 2*borderWidth, height:roundedLayer.bounds.height - 2*borderWidth )
v.layer.insertSublayer(roundedLayer, atIndex: 0)
v.layer.insertSublayer(solidLayer, above: roundedLayer)
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;