2013-04-07 83 views
1

我有一個CALayer(超級層),具有0.3的不透明屬性。超級圖層包含另一個CALayer(子圖層)。儘管子圖層不具有不透明屬性,但superLayer的不透明度會影響子圖層的外觀。有沒有一種方法可以覆蓋superLayer'a不透明屬性。CALayer,subLayer alpha覆蓋

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 

我已經加入了子視圖中的ViewController SuperView把。

+0

你是否找到了解決方案? – Carol 2016-12-13 16:19:56

回答

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對蘋果公司的文檔規定的繪圖沒有意義: /只有覆蓋drawRect:如果您執行自定義繪圖。 //一個空的實現在動畫過程中會對性能產生不利影響。