0

我正在使用 CAGradientLayer 来设置我的 uibuttons 样式。基本上在一个单元格中有三个按钮。当我首先设置 CAGradientLayer 属性时,它工作正常(我得到一个渐变)。

但是,当我将 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];
4

1 回答 1

0

你有一个复制粘贴错误。您正在创建两个不同的渐变层,但您为每个按钮添加了相同的一个 - 这一行:

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

应该读:

[commentButton.layer insertSublayer:commentBGLayer atIndex:0];
于 2013-02-15T20:55:24.640 回答