1

我有一个具有 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 中。

4

1 回答 1

1

根据您的问题,超级图层的不透明度控制子图层的不透明度,您可以将 CALayer 子类化并在 init 方法中覆盖它的一些属性,例如

@implementation SomeSuperCALayer

-(id)init
{
    self = [super init];
    if (self) {
        [self setup];
    }
    return  self;
}


#define  W_H 100.0
#define X_Y 50.0f

-(void)setup
{
    self.frame = CGRectMake(X_Y, X_Y, W_H, W_H);
    self.backgroundColor = [UIColor blueColor].CGColor;
    self.opacity = 1.0f; // big opacity 
}

@implementation SomeSubCALayer

-(id)init
{
    self = [super init];
    if (self) {
        [self setup];
    }
    return  self;
}


#define  W_H 100.0
#define X_Y 150.0f

-(void)setup
{
    self.frame = CGRectMake(X_Y, X_Y, W_H, W_H);
    self.backgroundColor = [UIColor blueColor].CGColor;
    self.opacity = .5f; // small opacity 
}


@ implementation MyViewController


// 
-(void)viewDidLoad
{
SomeSuperCALayer *superLayer = [[SomeSuperCALayer alloc] init];        SomeSubCALayer *subLayer = [[SomeSubCALayer alloc] init];
//add sub layer to super layer 
[superLayer addSubLayer:subLayer]; 
// add super layer to main View
[self.view.layer addSubLayer:superLayer]; 
// 
//... 


}

也不要使用 drawRect 来调用图层的属性,drawRect 并不像 Apple 的文档所规定的那样用于绘图:/仅覆盖 drawRect:如果您执行自定义绘图。// 空实现会对动画期间的性能产生不利影响。

于 2013-10-28T09:55:25.787 回答