2013-02-15 75 views
0

我正在使用CAGradientLayer來設計我的uibuttons。基本上在一個單元格中有三個按鈕。當我首先設置CAGradientLayer屬性時,它工作正常(我得到一個漸變)。CALayer QuartzCore問題

但是,當我將CAGradientLayer應用於第二個時,它確實出現在第二個上,但從第一個上消失。如果我在三分之一時間完成,那麼它將出現在第三個並從前兩個消失。

我應該在UIButton設置之間釋放CAGradientLayer嗎?

下面是一些示例代碼:

//like button 
self.likeButton=[[UIButton alloc] initWithFrame:CGRectMake(10, 430, 80, 26)]; 
[self.likeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12]]; 
[self.likeButton.titleLabel setTextAlignment:NSTextAlignmentLeft]; 
self.likeButton.tag=indexPath.row; 
[self.likeButton addTarget:self action:@selector(likeButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
[self.likeButton setBackgroundColor:[UIColor blackColor]]; 
[self.likeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[self.likeButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted]; 


CAGradientLayer *btnGradient=[CAGradientLayer layer]; 
btnGradient.frame=self.likeButton.bounds; 
btnGradient.colors=[NSArray arrayWithObjects:(id)[[UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0f] CGColor], (id)[[UIColor colorWithRed:41.0f/255.0f green:41.0f/255.0f blue:41.0f/255.0f alpha:1.0f]CGColor], nil]; 
[self.likeButton.layer insertSublayer:btnGradient atIndex:0]; 

CALayer *btnLayer=self.likeButton.layer; 
[btnLayer setMasksToBounds:YES]; 
[btnLayer setCornerRadius:5.0f]; 
[btnLayer setBorderWidth:1.0f]; 
[btnLayer setBorderColor:[[UIColor darkGrayColor]CGColor]]; 

[cell addSubview:self.likeButton]; 


//comment button 
UIButton *commentButton=[[UIButton alloc]initWithFrame:CGRectMake(110, 430, 80, 26)]; 
[commentButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12]]; 
[commentButton.titleLabel setTextAlignment:NSTextAlignmentLeft]; 
[commentButton setBackgroundColor:[UIColor blackColor]]; 
[commentButton setTitle:@"Comment" forState:UIControlStateNormal]; 
[commentButton addTarget:self action:@selector(commentButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
[commentButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
[commentButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted]; 


CAGradientLayer *commentBGLayer=[CAGradientLayer layer]; 
commentBGLayer.frame=commentButton.bounds; 
commentBGLayer.colors=[NSArray arrayWithObjects:(id)[[UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0f] CGColor], (id)[[UIColor colorWithRed:41.0f/255.0f green:41.0f/255.0f blue:41.0f/255.0f alpha:1.0f]CGColor], nil]; 
[commentButton.layer insertSublayer:btnGradient atIndex:0]; 


CALayer *commentBLayer=commentButton.layer; 
[commentBLayer setMasksToBounds:YES]; 
[commentBLayer setCornerRadius:5.0f]; 
[commentBLayer setBorderWidth:1.0f]; 
[commentBLayer setBorderColor:[[UIColor darkGrayColor]CGColor]]; 
[cell addSubview:commentButton]; 

回答

0

你已經有了一個複製粘貼錯誤。你創建了兩個不同的梯度層,但你要添加同一個每個按鈕 - 這條線:

[commentButton.layer insertSublayer:btnGradient atIndex:0]; 

應改爲:

[commentButton.layer insertSublayer:commentBGLayer atIndex:0];