【发布时间】:2011-11-11 01:18:07
【问题描述】:
我在子类UITableViewCell 中有一个子类UIButton。该按钮是从笔尖加载的。
我有一张图片想用作按钮的图片。我想使用CALayer 来更好地控制动画。当用户点击按钮时,会出现动画。但是,我什至无法显示图像。
QuartzCore 已导入。
我的子类UIButton 的代码(总是从笔尖加载):
-(void)awakeFromNib {
[super awakeFromNib];
self.clipsToBounds = NO;
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
self.userInteractionEnabled = YES;
}
表格视图控制器中的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell *cell = (BLCustomCellCenter*)[tableView dequeueReusableCellWithIdentifier:@"customCellID"];
if (cell == nil) {
// ... cell is initialized
}
// configure the image for the button
//
// Note: there is an outlet to the UIButton called 'customButton'
CALayer *imgLayer = [CALayer layer];
imgLayer.bounds = CGRectMake(0, 0, cell.customButton.bounds.size.width, cell.customButton.bounds.size.height);
imgLayer.position = CGPointMake(cell.customButton.bounds.size.width/2, cell.customButton.bounds.size.height/2);
UIImage *img = [UIImage imageNamed:@"someImage"];
imgLayer.contents = (id)[img CGImage];
[cell.customButton.layer addSublayer:imgLayer];
// ... configure subviews of 'customButton'
return cell;
}
任何帮助都非常感谢,一如既往。
【问题讨论】:
标签: iphone ios cocoa-touch core-animation calayer