QuartzCore 的 CALayer 问题

Posted

技术标签:

【中文标题】QuartzCore 的 CALayer 问题【英文标题】:CALayer issues with QuartzCore 【发布时间】:2013-02-15 20:47:45 【问题描述】:

我正在使用 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];

【问题讨论】:

【参考方案1】:

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

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

应阅读:

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

【讨论】:

以上是关于QuartzCore 的 CALayer 问题的主要内容,如果未能解决你的问题,请参考以下文章

转场动画CALayer (Transition)

Calayer子类问题

为啥即使没有 QuartzCore 也可以使用 Core Animation? [复制]

为啥 UIView 不需要 QuartzCore.framework 工作?

CALayer

UIView和CALayer