图层阴影的不透明度

发布于 2024-11-19 08:44:49 字数 1581 浏览 7 评论 0原文

我向 UIView 添加阴影:

[blurView.layer setCornerRadius:kCornerRadius];    
blurView.layer.masksToBounds = NO;
blurView.layer.shadowColor = [[UIColor redColor] CGColor];
blurView.layer.backgroundColor = blurView.layer.shadowColor;
blurView.layer.shadowPath = [UIBezierPath
                             bezierPathWithRoundedRect:CGRectMake(-2.0*kCornerRadius, -2.0*kCornerRadius, blurView.bounds.size.width+4.0*kCornerRadius, blurView.bounds.size.height+4.0*kCornerRadius)
                             cornerRadius:kCornerRadius].CGPath;
blurView.layer.cornerRadius = kCornerRadius;
blurView.layer.shadowOffset = CGSizeMake(0.0, 0.0);
blurView.layer.shadowRadius = kCornerRadius;
blurView.layer.shadowOpacity = kOpacity;
blurView.layer.opacity = 1.0;

这部分代码给了我一个这样的视图,就像 uiview 被模糊一样:

在此处输入图像描述

当我尝试更改blurView.layer的不透明度(例如更改为0.5)时,我得到了意想不到的视图:

在此处输入图像描述

正如你所看到的 - 我得到了 uiview 的锐利边缘。

我认为,阴影不透明度是图层不透明度的一部分 - 这是导致此“错误”的原因。有人能帮我解决这个问题吗?也许我可以在更改blurView.layer 的不透明度或类似的东西之前合并图层吗?

UPD

通过此修复: 模糊View.layer.shadowColor = [[UIColor colorWithRed:1 绿色:0 蓝色:0 alpha:0.5] CGColor];

我得到这个:

在此处输入图像描述

UPD

答案是使用 setShouldRasterize 方法:

在此输入图像描述

谢谢大家!

[blurView.layer setShouldRasterize:YES];

I add shadow to the UIView:

[blurView.layer setCornerRadius:kCornerRadius];    
blurView.layer.masksToBounds = NO;
blurView.layer.shadowColor = [[UIColor redColor] CGColor];
blurView.layer.backgroundColor = blurView.layer.shadowColor;
blurView.layer.shadowPath = [UIBezierPath
                             bezierPathWithRoundedRect:CGRectMake(-2.0*kCornerRadius, -2.0*kCornerRadius, blurView.bounds.size.width+4.0*kCornerRadius, blurView.bounds.size.height+4.0*kCornerRadius)
                             cornerRadius:kCornerRadius].CGPath;
blurView.layer.cornerRadius = kCornerRadius;
blurView.layer.shadowOffset = CGSizeMake(0.0, 0.0);
blurView.layer.shadowRadius = kCornerRadius;
blurView.layer.shadowOpacity = kOpacity;
blurView.layer.opacity = 1.0;

This part of code gives me a this view, like a uiview is blured:

enter image description here

When I try change opacity of blurView.layer (for example to 0.5), I get unexpected view:

enter image description here

As you can see - I get a sharp edges of uiview.

I think, shadow opacity is a part of opacity of layer - it's a cause of this 'error'. Can anybody help me to fix this? May be can I merge layers before change opacity of blurView.layer or something like this?

UPD

With this fix:
blurView.layer.shadowColor = [[UIColor colorWithRed:1 green:0 blue:0 alpha:0.5] CGColor];

I get this:

enter image description here

UPD

Answer is using setShouldRasterize method:

enter image description here

Thanks everybody!

[blurView.layer setShouldRasterize:YES];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏了南城 2024-11-26 08:44:49

答案是使用以下代码行:

blurView.layer.shouldRasterize = YES;

The answer is using this code line:

blurView.layer.shouldRasterize = YES;
山川志 2024-11-26 08:44:49

也许你可以尝试把这个放进去?

blurView.layer.shadowColor = [[UIColor colorWithRed:1 green:0 blue:0 alpha:0.5] CGColor];

Maybe you could try putting this in?

blurView.layer.shadowColor = [[UIColor colorWithRed:1 green:0 blue:0 alpha:0.5] CGColor];
成熟稳重的好男人 2024-11-26 08:44:49

如果你这样设置会发生什么:

blurView.layer.shadowOpacity = 1.0;
blurView.layer.opacity = 0.5;

我认为你可能将阴影不透明度设置为 0.5,然后将整个图层设置为 0.5,使阴影透明两倍?

(或者我完全错了!)

What happens if you put this :

blurView.layer.shadowOpacity = 1.0;
blurView.layer.opacity = 0.5;

I think that you might be setting the shadow opacity to 0.5 and then the whole layer to 0.5, making the shadow twice as transparent?

(or I'm completely wrong!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文