【发布时间】:2014-11-15 00:23:04
【问题描述】:
我正在尝试使用 CABasicAnimation 更改表格视图单元格中的标签,但它似乎不起作用 - 单元格背景颜色保持不变,没有动画。 这段代码在 cellForRowAtIndexPath 方法中
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MainCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *name = @"Hello";
UIColor *color = [UIColor colorWithRed:0.0f/255.0f green:100.0f/255.0f blue:200.0f/255.0f alpha:1.0f];
// I'm setting the label as a strong property of the cell
cell.label = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, cell.contentView.frame.size.height)];
cell.label.text = name;
cell.label.textColor = [UIColor whiteColor];
cell.label.backgroundColor = color;
[cell.contentView addSubview:cell.label];
UIColor *endColor = [UIColor redColor];
CABasicAnimation *animation;
animation=[CABasicAnimation animationWithKeyPath:@"backgroundColor"];
animation.duration=0.7;
animation.repeatCount=HUGE_VALF;
animation.autoreverses=YES;
animation.fromValue=(id)color.CGColor;
animation.toValue=(id)endColor.CGColor;
[cell.label.layer addAnimation:animation forKey:@"pulses"];
return cell;
}
【问题讨论】:
-
该代码对我来说很好用。你确定“标签”不是零。
-
@rdelmar - 你是在表格视图中这样做的吗?你可以发送示例代码吗?这在 tableviewcell 的上下文之外工作,但我还没有让它在 tableviewcell 中工作。
-
我复制了你的代码,并把它放在 cellFroRowAtIndexPath 中,就像你说的那样。我所做的唯一添加是添加一行来定义“颜色”(用于 fromValue),并且在最后一行中,“cell.label”而不是“label”(我的单元格有一个名为 label 的 IBOutlet)。你检查过“标签”不是零吗?
-
@rdelmar - 我刚刚在方法中添加了我的整个代码块。你能和你的比较一下,看看有什么不同吗?这不是动画...我在 iOS8 上运行它...
标签: ios uitableview quartz-core cabasicanimation