CAGradientLayer not draw the gradient correctly - ios

I use CAShapeLayer to draw a circle and set it as the mask of CAGradientLayer, here is the code:
CAShapeLayer *circleShapeLayer = [CAShapelayer new];
// draw circle path code here...
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = #[(__bridge id)[UIColor whiteColor].CGColor,
(__bridge id)[UIColor clearColor].CGColor];
// self here means a UIView
gradientLayer.mask = circleShapeLayer;
gradientLayer.frame = self.bounds;
[self.layer addSublayer:gradientLayer];
When I run the app, it will display a gradient circle, but the gradient is strange.
What I want is a circle that at start point, the color is white and at end point the color is clear color, it should look like this:
But the color of the circle in the Simulator screen is:
The color is symmetric.
I think the problem is that I do not set the gradientLayer.colors correctly, how can I fix it?

CAGradientLayer can not paint gradient along an arc. on the other hand, the mask layer's frame is too small than gradient layer's frame to see clear color

To Umair's response, it didn't make sense to me at first because even whiteColor or blackColor is being fetched by colorWithRed:green:blue:alpha but then I read this in the documentation:
When rendered, the colors are mapped to the output color space before being interpolated.
So maybe this is really it:
gradientLayer.colors = #[(__bridge id)[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.0f].CGColor,
(__bridge id)[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.0f].CGColor];

#J.Hunter's answer is correct in that CAGradientLayers cannot draw along an arc. This means that your drawn gradients will be limited to radial and linear. You are specifically looking to create an angled gradient, which I've attempted to do in the past as well.
Unfortunately, CAGradientLayer is bound to these limitations, and the best way I've found to create a masked angled gradient is to mask with a UIImage that contains an angled gradient. This won't be nearly as dynamic as drawing your own gradient, but it seems to be the best (possibly only) option at the moment.

Related

Is there a better way to detect and replace a gradient filled view in an IOS app when the device is rotated?

I recently fixed a bug with gradient filled backgrounds that occurs when I rotate the iPad.
I found that the gradient fill was the wrong size after rotation, leading to blocks of the screen not having the proper fill.
I detect device rotation as recommended in this question and invoke the following method each time a device orientation change is detected.
It works perfectly, but seems kind of kludgy to me.
My question is simple; Is this a best practice solution? What should I have done instead?
Edit: - per comments. Better code ?
/// Adds a gradient background layer to the root of the layer stack, and removes any other one that may already be there
+ (void) addGradientBackground:(UIView *)view {
dispatch_async(dispatch_get_main_queue(), ^{
CALayer *vl = view.layer.sublayers[0];
if ([vl isKindOfClass:[CAGradientLayer class]]) {
[vl removeFromSuperlayer];
}
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UICustomColor featherColor] CGColor], (id)[[UICustomColor marineColor] CGColor], nil];
gradient.startPoint = CGPointMake(0.0, 0.25);
gradient.endPoint = CGPointMake(1, 0.75);
[view.layer insertSublayer:gradient atIndex:0];
});
}

CAGradientLayer cannot change color

I want a colored gradient to overlay my view. In a view controller, I have this code
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor whiteColor];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.view.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:(id)[UIColor redColor].CGColor, (id)[UIColor clearColor].CGColor, nil];
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(0, 1.0f);
self.view.layer.mask = gradientLayer;
}
But even though the first color is red, I only ever see a black gradient. How can I display a red gradient instead?
TLDR: Instead of setting the gradient as the layer mask, add the gradient layer as a sublayer of view.layer.
Layers use the layer mask mask to determine the alpha of their own content by using the alpha of the mask at each pixel, since your gradientLayer is fully opaque, the effect you were getting wasn't the one you were hoping for.
Layers are similar to views (views are actually wrappers for layers), you can add them as sublayers in a similar way that views are added as subviews.

How I can make a gradient with three opposite points?

I want to make a color picker just like in the picture, but I could not do the triangle, which has three points to show the gradient, White, Black and the selected color.
The black and white are always the same, but the color selected should vary according to the user selects in the circle, but do not know how to make a gradient opposite to those three points, and displays the image.
(Only objective c, please)
Hope, someone could help me. Thanks.
HSL Picker
Here is what you can try to create gradient with three colors:
Get the reference from your triangle edges that have the reference to the color they point to.
BOOL isHorizontal=YES;
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[self.startColor CGColor],(id)[self.midColor CGColor], (id)[self.endColor CGColor], nil];
gradient.endPoint = (self.isHorizontal) ? CGPointMake(1, 0) : CGPointMake(0, 1);
[self.layer insertSublayer:gradient atIndex:0];
If you want to change the gradient direction, play around with:
[gradient setStartPoint:CGPointMake(0.0, 0.5)];
[gradient setEndPoint:CGPointMake(1.0, 0.5)];

Gradient text in a UITextField

I'm using a CAGradientLayer to make a gradient background for a text field. It looks like this
Instead of the background I want to have the gradient on the text. How can I make gradient text in a UITextField?
The code I used to make the gradient in the image
pinkDarkOp = [UIColor colorWithRed:0.9f green:0.53f blue:0.69f alpha:1.0];
pinkLightOp = [UIColor colorWithRed:0.79f green:0.45f blue:0.57f alpha:1.0];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = [[myTextField layer] bounds];
gradient.cornerRadius = 7;
gradient.colors = [NSArray arrayWithObjects:
(id)pinkDarkOp.CGColor,
(id)pinkLightOp.CGColor,
nil];
[[myTextField layer]addSublayer:gradient];
You can solve this by creating two layers:
First you create a layer which you fill with the gradient.
Then you create another layer containing the text.
Finally you use the second layer as a mask for masking the first layer. This is achieved by assigning the second layer to the mask property of the first layer.
If I look more specifically on your code example, I think that everything before the last line should be OK. But then you should replace the last line with something like this:
gradient.mask = myTextField.layer;
Perhaps you need to do some adjustments to myTextField as well (hard to say since its definition is not part of the listed code). It's important to understand that the text in the mask must be opaque but the rest of that layer must be fully transparent. Read about the "mask" property in the CALayer class reference:
https://developer.apple.com/library/ios/DOCUMENTATION/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instp/CALayer/mask
Dislaimer: I have not had time to test the code. I leave that to you. But the general idea of masking one layer with another should be possible to use, so I am convinced that you can sort out the details by yourself.
i made somme changes in my codes
pinkDarkOp = [UIColor colorWithRed:0.9f green:0.53f blue:0.69f alpha:1.0];
pinkLightOp = [UIColor colorWithRed:0.79f green:0.45f blue:0.57f alpha:1.0];
gradient.frame = [[myTextField layer] bounds];
gradient.cornerRadius = 7;
gradient.colors = [NSArray arrayWithObjects:(id)pinkDarkOp.CGColor,
(id)[[UIColor clearColor] CGColor], nil];
gradient.mask=myTextField.layer;
the text is invisible now

CAGradientLayer covers everything drawn in drawRect

I have the following code:
-(void)drawRect:(CGRect)rect
{
// gradient background
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = rect;
gradient.colors = [NSArray arrayWithObjects:(id) backgroundGradientTop.CGColor, (id) backgroundGradientBottom.CGColor, nil];
gradient.locations = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.7], nil];
[self.layer insertSublayer:gradient atIndex:0];
// line on top
[[UIColor redColor] set];
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(currentContext, 5.0f);
CGContextMoveToPoint(currentContext, 0, 10.0f);
CGContextAddLineToPoint(currentContext, rect.size.width, 10.0f);
CGContextStrokePath(currentContext);
}
the line i'm trying to draw on top of the gradient is never shown. If i comment out the gradient layer it is there. Is there someway to draw both a gradient background and a line (or a few lines) on top? Maybe i shouldn't be mixing calayer and CG?
The line i'm trying to draw on top of the gradient is never shown. If i comment out the gradient layer it is there.
That's because sublayers appear on top of their parent layers. Your gradient is apparently opaque, and the same size as your view, so it covers up the view.
You can't mix CA and CG drawing this way. It would work better if you drew the gradient using CGContextDrawLinearGradient.

Resources