图层阴影的不透明度

时间:2011-07-11 15:28:13

标签: iphone uiview shadow quartz-core

我在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模糊了:

enter image description here

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

enter image description here

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

我认为,阴影不透明度是图层不透明度的一部分 - 这是“错误”的原因。任何人都可以帮我解决这个问题吗?我可以在更改blurView.layer的不透明度之前合并图层吗?

UPD

使用此修复程序:     blurView.layer.shadowColor = [[UIColor colorWithRed:1 green:0 blue:0 alpha:0.5] CGColor];

我明白了:

enter image description here

UPD

答案是使用setShouldRasterize方法:

enter image description here

谢谢大家!

[blurView.layer setShouldRasterize:YES];

3 个答案:

答案 0 :(得分:3)

答案是使用此代码行:

blurView.layer.shouldRasterize = YES;

答案 1 :(得分:0)

也许你可以试试这个吗?

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

答案 2 :(得分:0)

如果你这样做会发生什么:

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

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

(或者我完全错了!)

相关问题