【发布时间】:2014-03-15 15:16:03
【问题描述】:
我有一个带有卡片的 UITableView。如果一张牌是可玩的,背景颜色是绿色,如果不是,背景颜色是红色。我希望它以脉动的方式制作动画,并且我已经做到了:
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
Card *card;
card = [[game playerCards] objectAtIndex:indexPath.row];
if(card.playable == IsPlayable){
[UIView animateKeyframesWithDuration:0.9 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction animations:^{
cell.backgroundColor = [UIColor colorWithRed:0.105882F green:0.564706F blue:0.243137F alpha:1.0F];
cell.backgroundColor = [UIColor colorWithRed:0.105882F green:0.564706F blue:0.243137F alpha:0.0F];
completion:^(BOOL finished) {
}];}
else if (card.playable == IsNotPlayable){
[UIView animateKeyframesWithDuration:0.9 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction animations:^{
cell.backgroundColor = [UIColor colorWithRed:1.000000F green:0.000000F blue:0.090196F alpha:1.0F];
cell.backgroundColor = [UIColor colorWithRed:1.000000F green:0.000000F blue:0.090196F alpha:0.0F];
completion:^(BOOL finished) {
}];}
}
效果很好,但是滚动后动画不同步。因此,当 viewdidload 完成时,所有可见单元格都在同步跳动,但在我滚动表格后,动画不再同步,这会产生丑陋的效果。
我通过在 scrollViewDidScroll 中调用 [playerCardsTable reloadData] 设法克服了这个问题。现在,当我滚动表格时,动画停止并以完整的 alpha 为其提供正确的颜色,当滚动停止时,它再次开始同步脉动。这正是我想要的!但这似乎是一种非常“昂贵的方式”。 CPU 在滚动期间达到 12% 的峰值(Xcode 中的 CPU 指示器)。我也尝试使用 Timer 启动动画,但无济于事。
我应该在滚动期间继续重新加载表格数据吗?还是有其他方法?还有一个问题,12% 是不是很多(Mac Mini i7)?
谢谢!
【问题讨论】:
-
您不应该重新发布您的问题,而应该编辑较旧的问题。 stackoverflow.com/questions/22373185/…
-
我的错,删除了旧的。
-
@Niels,我只是想知道为什么不将您的代码放在 cellForRow 中?
标签: ios uitableview