动画 UITableViewCell 背景色 [英] Animation UITableViewCell backgroundcolor

查看:30
本文介绍了动画 UITableViewCell 背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有卡片的 UITableView.如果一张卡可以玩,背景颜色是绿色,如果不是,背景颜色是红色.我希望以一种脉动的方式制作动画,而且我已经做到了:

I have a UITableView with cards in it. If a card is playable, the background color is green, if not, the background color is red. I want this to be animated in a pulsating way and I already managed to do so:

-(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 完成时,所有可见单元格都在同步脉动,但在我滚动表格后,动画不再同步,这会产生丑陋的效果.

It is working really nice, but, the animation is not in sync after scrolling. So when viewdidload is finished, all visible cells are pulsating in sync, but after I scroll the table the animation is not in sync anymore, which gives an ugly effect.

通过在 scrollViewDidScroll 中调用 [playerCardsTable reloadData],我设法克服了这个问题.现在,当我滚动表格时,动画停止并以完全 alpha 的形式为其提供正确的颜色,当滚动停止时,它又开始同步脉动.这正是我想要的!但这似乎是一种非常昂贵的方式".CPU 在滚动期间达到 12% 的峰值(Xcode 中的 CPU 指示器).我也试过用定时器启动动画,但无济于事.

I managed to overcome this, by calling [playerCardsTable reloadData] in scrollViewDidScroll. Now when I scroll the table the animation stops and gives it the correct color in full alpha and when scrolling is stopped it starts pulsating again in sync. This is exactly what I want! But this seems to be a very "expensive way". CPU is peaking at 12% during scrolling (CPU indicator in Xcode). I also tried starting the animation with a Timer, but to no avail.

我应该在滚动期间继续重新加载表格数据吗?或者还有其他方法吗?还有一个附带问题,12% 是不是很多(Mac Mini i7)?

Should I just go on with reloading the table data during scrolling? Or is there another way? And a side question, is 12% a lot (Mac Mini i7)?

谢谢!

推荐答案

这是我的建议

将这 2 个属性添加到您的控制器.此属性将管理 alpha 的百分比

Add this 2 properties to your controller. This properties will manage the % of alpha

@property CGFloat alpha;
@property CGFloat alphaInc; //alpha will be incremented/decremented by this value

在您的 viewDidLoad 中,初始化属性,并创建一个用于更新背景的计时器

In your viewDidLoad, init the properties, and create a timer for updating the backgrounds

self.alphaInc = 0.01;
self.alpha = 0.1;

[NSTimer scheduledTimerWithTimeInterval:0.05
                                 target:self
                               selector:@selector(updateMyCells)
                               userInfo:nil
                                repeats:YES];

添加将更新单元格的方法.我已经考虑到您正在使用 UITableViewController,否则请为您的 Table 视图创建一个 iBoutlet 并将其命名为 tableview.

add the method that will update the cells. I've took in consideration your are using a UITableViewController, otherwise please create an iBoutlet for your Table view and name it tableview.

-(void)updateMyCells {
    self.alpha += self.alphaInc;
    if (self.alpha > .5 || self.alpha < .1) {
        self.alphaInc *= -1;
        self.alpha += self.alphaInc;
    }

    [[self.tableView visibleCells] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if (obj) {
            UITableViewCell *cell = (UITableViewCell*)obj;
            cell.backgroundColor = [cell.backgroundColor colorWithAlphaComponent:self.alpha];
        }
    }];
}

最后,调整你的 tableView:willDisplayCell:forRowAtIndexPath: :

and finally, adjust your tableView:willDisplayCell:forRowAtIndexPath: :

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    Card *card;
    card = [[game playerCards] objectAtIndex:indexPath.row];

    if(card.playable == IsPlayable){
        cell.backgroundColor = [UIColor colorWithRed:0.105882F green:0.564706F blue:0.243137F alpha:1.0F];
    else {
        cell.backgroundColor = [UIColor colorWithRed:1.000000F green:0.000000F blue:0.090196F alpha:1.0F];
    }
}

这篇关于动画 UITableViewCell 背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆