Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blending CAGradientLayer used as shadow

I have a CAGradientLayer I'm using as a shadow. It fades from 60% opacity to clear, left to right. On the edge of the gradient, it appears to be blending with the layer beneath it and lightening that layer:

Notice the ~1 pixel wide lightened effect on the photo beneath the shadow

There's a one-pixel wide "glow" at the very end of the shadow, where it's faded to clear. It seems to make the photo behind the shadow lighter at the tail of the shadow. Here's a close-up:

enter image description here

This is the code that produces that shadow:

CAGradientLayer *layer = [CAGradientLayer layer];
layer.frame = CGRectMake(sideBar.frame.size.width, 0, 7, sideBar.frame.size.height);
layer.colors = [NSArray arrayWithObjects:
                (id)[[[UIColor blackColor] colorWithAlphaComponent:0.6f] CGColor],
                (id)[[UIColor clearColor] CGColor],
                nil];
[layer setStartPoint:CGPointMake(0, 0.5)];
[layer setEndPoint:CGPointMake(1, 0.5)];
[sideBar.layer insertSublayer:layer atIndex:0];  
sideBar.layer.masksToBounds = NO;

Any ideas on how to get rid of that?

like image 674
Ash Furrow Avatar asked Nov 01 '11 20:11

Ash Furrow


1 Answers

Your eyes are deceiving you. There's no increase in intensity at the edge of the gradient. You can check- I shrunk the image to 1px height in the cloud region and looked at the color values. image shrunk to 1px height

However, the reason you're seeing a bright line is that shadows don't look like that. The transparency profile that you'd expect to see would be closer to a gaussian than a straight line (which is what the linear gradient does).

I'd recommend adding a few extra points to ease the gradient into the transparent region. Perhaps something like this

layer.colors = [NSArray arrayWithObjects:
                (id)[[[UIColor blackColor] colorWithAlphaComponent:0.6f] CGColor],
                (id)[[[UIColor blackColor] colorWithAlphaComponent:0.3f] CGColor],
                (id)[[[UIColor blackColor] colorWithAlphaComponent:0.1f] CGColor],
                (id)[[UIColor clearColor] CGColor],
                nil];

You might have to increase the size of the gradient to accomodate this extra blending.

like image 149
joerick Avatar answered Nov 17 '22 10:11

joerick