CALayer,subLayer alpha覆盖

时间:2013-04-07 17:33:35

标签: ios calayer opacity alpha quartz-2d

我有一个具有0.3不透明度属性的CALayer(超级图层)。超级层包含另一个CALayer(子层)。虽然子图层没有不透明度属性,但superLayer的不透明度会影响子图层的外观。有没有一种方法可以覆盖superLayer'不透明度属性。

SuperLayer

@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。

1 个答案:

答案 0 :(得分: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:如果执行自定义绘图。 //空实现会对动画期间的性能产生负面影响。