图层阴影的不透明度
我向 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:
When I try change opacity of blurView.layer (for example to 0.5), I get unexpected view:
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:
UPD
Answer is using setShouldRasterize method:
Thanks everybody!
[blurView.layer setShouldRasterize:YES];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案是使用以下代码行:
The answer is using this code line:
也许你可以尝试把这个放进去?
Maybe you could try putting this in?
如果你这样设置会发生什么:
我认为你可能将阴影不透明度设置为 0.5,然后将整个图层设置为 0.5,使阴影透明两倍?
(或者我完全错了!)
What happens if you put this :
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!)