CALayer与QuartzCore有关

时间:2013-02-15 20:47:46

标签: ios xcode calayer quartz-core cagradientlayer

我正在使用CAGradientLayer来设置我的uibutton样式。一个单元格中基本上有三个按钮。当我首先设置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];

1 个答案:

答案 0 :(得分:0)

您遇到了复制粘贴错误。您正在创建两个不同的渐变图层,但是您要为每个按钮添加相同的渐变图层 - 此行:

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

应阅读:

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