首页 文章

图层阴影的不透明度

提问于
浏览
2

我给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

    答案是使用此代码行:

    blurView.layer.shouldRasterize = YES;
    
  • 0

    也许你可以试试这个吗?

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

    如果你把这个怎么办:

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

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

    (或者我完全错了!)

相关问题