我希望我的NSView同时具有渐变背景和阴影,因此我将NSView
子类化并覆盖了- (void)awakeFromNib
方法,如下所示:
[self setWantsLayer:YES]; // view's backing store is using a Core Animation Layer
CAGradientLayer *backgroundGradient = [[CAGradientLayer alloc] init];
backgroundGradient.colors = @[
(__bridge id)CGColorCreateGenericGray(0.85, 1.0),
(__bridge id)CGColorCreateGenericGray(0.94, 1.0)
];
backgroundGradient.masksToBounds = NO;
CALayer *shadowLayer = [CALayer layer];
shadowLayer.shadowColor = CGColorCreateGenericGray(0.5, 1.0);
shadowLayer.shadowOffset = NSMakeSize(0, 0.0);
shadowLayer.shadowRadius = 10.0;
shadowLayer.shadowOpacity = 1.0;
shadowLayer.masksToBounds = NO;
[backgroundGradient addSublayer:shadowLayer];
[self setLayer:backgroundGradient];
但是,只显示了渐变。此外,如果我尝试[self setLayer:shadowLayer]
,则显示阴影(当然,没有渐变)。
如何显示渐变和阴影?非常感谢!