objective-c - 混合 CAGradientLayer 用作阴影

标签 objective-c ios core-animation calayer

我有一个 CAGradientLayer 用作阴影。它从 60% 的不透明度逐渐变为透明,从左到右。在渐变的边缘,它似乎与它下面的层混合并使该层变亮:

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

在阴影的最末端有一个一像素宽的“发光”,它从那里褪色到清晰。它似乎使阴影后面的照片在阴影的尾部变亮。这是一个特写:

enter image description here

这是产生阴影的代码:

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;

关于如何摆脱它的任何想法?

最佳答案

你的眼睛在欺骗你。梯度边缘的强度没有增加。你可以检查——我在云区域将图像缩小到 1px 高度并查看了颜色值。 image shrunk to 1px height

但是,您看到亮线的原因是阴影看起来不像那样。您希望看到的透明度曲线更接近于高斯曲线而不是直线(这就是线性渐变的作用)。

我建议添加一些额外的点以缓和透明区域的渐变。也许是这样的

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

您可能需要增加渐变的大小以适应这种额外的混合。

关于objective-c - 混合 CAGradientLayer 用作阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7972755/

相关文章:

ios - 如何在 iOS 中完成循环后停止音频

iphone - 识别面部并使其变形

ios - UIView 动画与 CALayers

iphone - iPhone 和 iPad 中的缓存目录有多大?

objective-c - 为什么 NSOpenPanel/NSSavePanel 显示内存泄漏?

ios - AVAudioRecorder 录制流式音频

ios - 带按钮的可滚动菜单部分

ios - 快速动画旋转 UIImageView

ios - iOS中如何实现 "clock wipe"/径向删除效果?

iphone - CAKeyframeAnimation崩溃