我有一个具有 0.3 opacity 属性的 CALayer ( superlayer )。超层包含另一个 CALayer(子层)。虽然 sublayer 没有 opacity 属性 superLayer 的 opacity 会影响 sublayer 的外观。有没有一种方法可以覆盖 superLayer'a 不透明度属性。
超层
@implementation SuperView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setOpaque:NO];
[self.layer setOpaque:NO];
}
return self;
}
-(void)drawRect:(CGRect)rect
{
CALayer* superLaer=self.layer;
superLaer.backgroundColor = [UIColor blackColor].CGColor;
superLaer.shadowOffset = CGSizeMake(0, 3);
superLaer.shadowRadius = 5.0;
superLaer.shadowColor = [UIColor blackColor].CGColor;
superLaer.shadowOpacity = 0.6;
superLaer.frame = CGRectMake(10, 10, 300,300);
superLaer.borderColor = [UIColor blackColor].CGColor;
superLaer.borderWidth = 2.0;
superLaer.cornerRadius = 10.0;
superLaer.cornerRadius = 10.0;
superLaer.masksToBounds=YES;
superLaer.opacity = 0.1;
}
@end
子层
@implementation SubView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setOpaque:NO];
[self.layer setOpaque:NO];
}
return self;
}
-(void)drawRect:(CGRect)rect
{
CALayer* superLaer=self.layer;
superLaer.backgroundColor = [UIColor redColor].CGColor;
superLaer.shadowOffset = CGSizeMake(0, 3);
superLaer.shadowRadius = 5.0;
superLaer.shadowColor = [UIColor blackColor].CGColor;
superLaer.shadowOpacity = 0.6;
superLaer.frame = CGRectMake(100, 100, 100,100);
superLaer.borderColor = [UIColor blackColor].CGColor;
superLaer.borderWidth = 2.0;
superLaer.cornerRadius = 10.0;
superLaer.cornerRadius = 10.0;
superLaer.masksToBounds=YES;
}
@end
我已将 subView 添加到 Viewcontroller 中的 superView 中。