14

我有一个带有半透明填充和阴影的 UIView。由于填充是半透明的,我可以看到填充后面的阴影。

- (id)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(0.0, 0.0);
        self.layer.shadowOpacity = 0.5;
        self.layer.shadowRadius = 2.0;
        self.layer.cornerRadius = 3.0;
    }
    return self;
}

我不喜欢这种行为。我看不到视图后面的任何东西,因为填充的不透明度 + 阴影的不透明度 > 100%。如何让它像 CSS 一样,阴影只绘制在框的边框之外?

.someStyle {
   background: white;
   opacity: 0.8;
   box-shadow: 0 0 1em rgba(0,0,0,0.5);
}
4

1 回答 1

5

我能够使用以下内容获得这种预期的效果:

...
someLayer.backgroundColor = [[UIColor greenColor] CGColor];
someLayer.shadowOpacity = 1.0;
someLayer.shadowOffset = CGSizeMake(10.0, 10.0);
someLayer.shadowColor = [[UIColor blackColor] CGColor];

someLayer.rasterizationScale = [[UIScreen mainScreen] scale];
someLayer.shouldRasterize = YES;
someLayer.opacity = 0.5;

[[self layer] addSublayer:someLayer];
于 2012-12-28T20:08:40.897 回答