如何为 UITableViewCell 的颜色设置动画? [英] How can I animate a UITableViewCell's colors?

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

问题描述

我希望允许用户在应用程序中的几个调色板之间切换,IE 如何让 XCode 和其他文本编辑器在浅色和深色背景之间切换.我可以很简单地完成所有这些工作,但是现在我试图将更改包装在一个简单的动画块中,以便颜色更改淡入.除了我的表格视图单元格之外,一切都很好.颜色会改变,但不会产生动画.

I want to allow the user to switch between a few color palettes in the application, IE how XCode and other text editors allow you to switch between light and dark backgrounds. I'm able to do all this simply enough, but now I'm trying to wrap the change in a simple animation block so the color changes fade in. Everything works great with the exception of my table view cells. The colors change, but they don't animate.

[UIView animateWithDuration:0.5 animations:^{
    self.tableView.backgroundColor = [UIColor whiteColor];

    for (UITableViewCell *cell in self.tableView.visibleCells)
    {
        cell.textLabel.textColor = [UIColor blueColor];
    }
}];

我试图不重新加载整个表格,因为这会导致很多我不想要的东西重新布局.坦率地说,我已经尝试了几次,但无论如何它都不起作用.

I'm attempting not to reload the entire table, as that will cause a lot of things to layout again which I don't want. Frankly, I've tried it a couple times and it doesn't work anyway.

就其价值而言,我的 UITableView 已分组,但我认为这不会真正影响我的解决方案.

For what it's worth, my UITableView is grouped, though I don't think that's really effecting my solution.

此处列出的答案很有趣,但我认为与我的问题无关 - 我正在更改从不动画的文本颜色:在块动画中为UITableViewCell的backgroundColor设置动画

The answers listed here are interesting, but I don't believe are relevant to my problem - I am changing text color which never animates: Animating UITableViewCell's backgroundColor in block animation

推荐答案

谢谢大家,@shadowhorst 链接真的很有帮助.我能够通过使用交叉溶解过渡而不是 animateWithDuration 方法来完成我想要的.代码如下 -

Thanks guys, @shadowhorst link was really helpful. I was able to accomplish what I wanted by using a cross-dissolve transition instead of the animateWithDuration method. The code is below -

[UIView transitionWithView:self.tableView
                  duration:0.25
                   options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowAnimatedContent
                animations:^{
                    self.tableView.backgroundColor = [UIColor whiteColor];;

                    for (UITableViewCell *cell in self.tableView.visibleCells)
                    {
                        cell.textLabel.textColor = [UIColor blueColor];
                    }
                }
                completion:nil];

我确实注意到分组单元格的边缘仍然存在一些伪影,这就是我将动画的持续时间缩短到四分之一秒的原因.

I did notice that there are still some artifacts around the edges of my grouped cells, which is why I dropped the duration of my animation down to a quarter of a second.

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

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